Map is a set interface of key-value pairs, and its implementation classes mainly include: Hashmap,treemap,hashtable and Linkedhashmap.
- TREEMAP: Based on the navigablemap implementation of the red-black tree (red-black trees), the map is sorted according to the natural order of its keys or sorted based on the Comparator provided when the mapping was created, depending on the construction method used.
- The value of HashMap is not sequential, it is according to the key hashcode to achieve, for this unordered hashmap how do we want to implement the sort? Sorts the value of reference TreeMap.
Map.entry returns the collections view.
Sort by key
TreeMap is ascending by default, and if we need to change the sorting method, we need to use the comparator: Comparator. Comparator a comparer interface that can sort a collection object or array, implementing the public compare (T O1,to2) method of the interface to achieve sorting.
Note: The following code has been passed in the Jdk1.6 test
TreeMap by key in ascending order by default
Public Static voidKeyupsort () {//By default, TreeMap is sorted in ascending order of keymap<string, integer> map =NewTreemap<string, integer>(); 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); //by default, TreeMap the key in ascending orderSystem.out.println ("------------Normal, TreeMap sorted by key ascending--------------------"); for(Map.entry<string, integer>Entry:map.entrySet ()) {System.out.println (Entry.getkey ()+ ":" +Entry.getvalue ()); } }
To modify the sort of treemap, sort by key in descending order
Public Static voidKeydownsort () {//TreeMap, sort by key descending//Descending sort ComparerComparator<string> Keycomparator =NewComparator<string>() {@Override Public intCompare (String O1, String O2) {//TODO auto-generated Method Stub returnO2.compareto (O1); } }; Map<string, integer> map =NewTreemap<string, integer>(Keycomparator); 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); 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 shows the sort by value ascending by TreeMap, which also applies to HashMap.
To modify the sort method of TreeMap, sort by value ascending
Note: Normally, map cannot be sorted using the Collections.sort () method, but you can convert the map to list before sorting.
Public Static voidValueupsort () {//By default, TreeMap is sorted in ascending order of keymap<string, integer> map =NewTreemap<string, integer>(); 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 Comparatorcomparator<map.entry<string, integer>> valuecomparator =NewComparator<map.entry<string,integer>>() {@Override Public intCompare (Entry<string, integer>O1, Entry<string, integer>O2) { //TODO auto-generated Method Stub returnO1.getvalue ()-O2.getvalue (); } }; //map conversion to list for sortinglist<map.entry<string, integer>> list =NewArraylist<map.entry<string,integer>>(Map.entryset ()); //SortCollections.sort (List,valuecomparator); //by default, TreeMap the key in ascending orderSYSTEM.OUT.PRINTLN ("------------map sorted in ascending order of value--------------------"); for(Map.entry<string, integer>entry:list) {System.out.println (Entry.getkey ()+ ":" +Entry.getvalue ()); } }
Test results
------------Normal, TreeMap is sorted by key in ascending order--------------------ABC1:10ABC2:12ACB1:5BAC1:3BCA1:20CAB1:80CBA1:1------------treemap sorted by key in descending order--------------------CBA1:1CAB1:80BCA1:20BAC1:3ACB1:5ABC2:12ABC1:------------map is sorted in ascending order of value--------------------CBA1:1BAC1:3ACB1:5ABC1:10ABC2:12BCA1:20CAB1:80
Java map sorted by value