Map is a collection interface for key-value pairs. Its implementation classes mainly include: HashMap, TreeMap, Hashtable, and LinkedHashMap.
•TreeMap: A NavigableMap implementation based on the Red-Black tree, which is sorted in the natural order of its keys or in the Comparator provided when creating the map, depending on the constructor used.
•The HashMap values are in order, they are implemented according to the HashCode of the key. How do we implement sorting for this unordered HashMap? Refer to the value sort of TreeMap.
Map.Entry returns the Collections view.
Sort by key
TreeMap is in ascending order by default. If we need to change the sorting method, we need to use a comparator: Comparator. Comparator can sort the comparator interface for collection objects or arrays, and implement the public comparison(T o1,To2) method of this interface to realize sorting.
Note: All the following codes have been tested in Jdk1.6
TreeMap is sorted in ascending order by default
public static void keyUpSort() {// By default, TreeMap is sorted in ascending order of keyMap<String, Integer> map = new TreeMap<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 sorts the keys in ascending order System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Modify the sorting method of TreeMap and sort by descending key
public static void keyDownSort() {// TreeMap, sorted by descending order // Sort in descending order Comparator<String> keyComparator = new Comparator<String>() {@Overridepublic int compare(String o1, String o2) {// TODO Auto-generated method stubreturn o2.compareTo(o1);}};Map<String, Integer> map = new TreeMap<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按key降序排序--------------------");for (Map.Entry<String, Integer> entry : map.entrySet()) {System.out.println(entry.getKey() + ":" + entry.getValue());}}Sort by Value
The following only demonstrates sorting by TreeMap by ascending order, and the same applies to HashMap.
Modify the sorting method of TreeMap and sort it in ascending order of Value
Note: Under normal circumstances, Maps cannot be sorted using the Collections.sort() method, but Maps can be converted into list before sorting.
public static void valueUpSort() {// By default, TreeMap is sorted in ascending order by key Map<String, Integer> map = new TreeMap<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 comparator Comparator<Map.Entry<String, Integer>> valueComparator = new Comparator<Map.Entry<String,Integer>>() {@Overridepublic int compare(Entry<String, Integer> o1,Entry<String, Integer> o2) {// TODO Auto-generated method stubreturn o1.getValue()-o2.getValue();}};// Convert map 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 sorts keys in ascending order System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Test results
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The above is the implementation method of Java Map sorting according to Value introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!