Android HashMap two ways to sort by key value

Source: Internet
Author: User

Like the next Map object:

Hashmap<integer, string> map = new Hashmap<integer, string> ();

Map.put (3, "A");

Map.put (2, "B");

Map.put (1, "C");

The first method of ordering key values:

object[] key =  Map.keyset (). ToArray ();  Arrays.sort (key);    for (int0; i<key.length; i++) {       log.i ("sang" "  value=>"+map. Get (Key[i]));  }

Key-Value ordering the second method:

List arrayList =NewArrayList (Map.entryset ()); Collections.sort (ArrayList,NewComparator () { Public intCompare (object Arg1, Object arg2) {map.entry obj1=(map.entry) arg1; Map.entry Obj2=(map.entry) arg2; return(Obj1.getkey ()). ToString (). CompareTo (Obj2.getkey ()); }});//sort the data in the HashMap for(Iterator iter =arraylist.iterator (); Iter.hasnext ();) {Map.entry Entry=(Map.entry) iter.next (); String Key=(String) Entry.getkey (); LOG.I ("sang","value ="+map.Get(key)); }

Android HashMap two ways to sort by key value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.