The implementation method of Java Map in order of value _java

Source: Internet
Author: User

Map is the set interface of key value pairs, and its implementation classes mainly include: Hashmap,treemap,hashtable and Linkedhashmap.

TreeMap: A NAVIGABLEMAP implementation based on the red-black Tree (red-black), sorted according to the natural order of its keys, or sorted according to the Comparator provided when the mapping was created, depending on the construction method used.

HashMap value is not the order, it is according to the hashcode of key to achieve, for this disorderly hashmap how do we want to achieve the sort? The value of the reference TreeMap is sorted.

Map.entry returns the collections view.

Sort by key

TreeMap default is ascending, and if we need to change the sort, we need to use the comparer: Comparator. A comparer interface that comparator the collection object or array can be sorted by implementing the public compare (T O1,to2) method of the interface.

Note: The following code has been passed in the Jdk1.6 test

TreeMap default by key in ascending order

public static void Keyupsort () {
//default, TreeMap sorted by key in ascending order
map<string, integer> Map = new treemap<string, Integer> ();
Map.put ("ACB1", 5);
Map.put ("Bac1", 3);
Map.put ("BCA1");
Map.put ("Cab1");
Map.put ("Cba1", 1);
Map.put ("ABC1");
Map.put ("ABC2");
By default, TreeMap the key in ascending order
System.out.println ("------------Normal, TreeMap sorted in key ascending order--------------------");
For (map.entry<string, integer> entry:map.entrySet ()) {
System.out.println (Entry.getkey () + ":" + Entry.getvalue ());
}

Modify how TreeMap is sorted, sorted by key in descending order

public static void Keydownsort () {
//TreeMap, sorted by key descending sort
//descending sort comparer
comparator<string> keycomparator = New Comparator<string> () {
@Override public
int Compare (string O1, String O2) {
//TODO auto-generated Method stub return
O2.compareto (O1);
}
;
map<string, integer> map = new treemap<string, integer> (keycomparator);
Map.put ("ACB1", 5);
Map.put ("Bac1", 3);
Map.put ("BCA1");
Map.put ("Cab1");
Map.put ("Cba1", 1);
Map.put ("ABC1");
Map.put ("ABC2");
System.out.println ("------------treemap sorted by key in descending order--------------------");
For (map.entry<string, integer> entry:map.entrySet ()) {
System.out.println (Entry.getkey () + ":" + Entry.getvalue ());
}

Sort by value

The following only demonstrates pressing TREEMAP by value in ascending order, which is also true for HashMap.

Modify how TreeMap is sorted, sorted by value in ascending order

Note: Normally a map cannot be sorted by using the Collections.sort () method, but it can be sorted after the map is converted to a list .

public static void Valueupsort () {//default, TreeMap sorted by key in ascending order map<string, integer> Map = new treemap<string, intege
R> ();
Map.put ("ACB1", 5);
Map.put ("Bac1", 3);
Map.put ("BCA1", 20);
Map.put ("Cab1", 80);
Map.put ("Cba1", 1);
Map.put ("ABC1", 10);
Map.put ("ABC2", 12); Ascending comparer comparator<map.entry<string, integer>> valuecomparator = new comparator<map.entry<string, Integer>> () {@Override public int compare (entry<string, integer> O1, entry<string, integer> O2) {/To
Do auto-generated method stub return O1.getvalue ()-o2.getvalue ();
}
}; Map converted to list for sorting list<map.entry<string, integer>> list = new Arraylist<map.entry<string,integer
>> (Map.entryset ());
Sort Collections.sort (List,valuecomparator);
By default, TreeMap the key in ascending order System.out.println ("------------map--------------------sorted by value ascending)"; For (map.entry<string, integer> entry:list) {System.out.println (Entry.getkey () + ":" + entry.getvalue ());}

Test results

------------Normal, TreeMap sorted by key in ascending order--------------------
abc1:10
abc2:12
acb1:5
bac1:3
bca1:20
cab1:80
cba1:1
------------treemap sorted by key in descending order--------------------
cba1:1
cab1:80
BCA1 :
bac1:3
acb1:5
abc2:12
abc1:10
------------map sorted by value Ascending--------------------
cba1:1
bac1:3
acb1:5
abc1:10
abc2:12
bca1:20

The above is a small series to introduce the Java Map in accordance with the implementation of the value of the method, I hope to help everyone, if you have any questions please give me a message, small set will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.