1. Theoretical preparation
Map is a collection interface of key-value pairs. Its implementation classes mainly include: HashMap, TreeMap, Hashtable and LinkedHashMap, etc.
TreeMap: A NavigableMap implementation based on a Red-Black tree, which is sorted according to the natural ordering of its keys, or according to the Comparator provided when the map is created, depending on the construct used method.
The value of HashMap is not in order. It is implemented according to the HashCode of the key. How do we implement sorting for this unordered HashMap? Refer to the value sorting of TreeMap.
Map.Entry returns the Collections view.
2. Key sorting
TreeMap is in ascending order by default. If we need to change the sorting method, we need to use a comparator: Comparator. Comparator is a comparator interface that can sort collection objects or arrays. Implementing the public compare(T o1, To2) method of this interface can achieve sorting, as follows:
import java.util.Comparator; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeMap; public class TreeMapTest { public static void main(String[] args) { Map<String, String> map = new TreeMap<String, String>( new Comparator<String>() { public int compare(String obj1, String obj2) { // 降序排序 return obj2.compareTo(obj1); } }); map.put("b", "ccccc"); map.put("d", "aaaaa"); map.put("c", "bbbbb"); map.put("a", "ddddd"); Set<String> keySet = map.keySet(); Iterator<String> iter = keySet.iterator(); while (iter.hasNext()) { String key = iter.next(); System.out.println(key + ":" + map.get(key)); } } }
The running results are as follows:
d:aaaaa c:bbbbb b:ccccc a:ddddd
3. Value sorting
The above example is to sort based on the key value of TreeMap, but sometimes we need to sort based on the value of TreeMap. To sort values, we need to use the sort(List8742468051c85b06f0a0af9e3e506b5c list, Comparator117c5a0bdb71ea9a9d0c2b99b03abe3e c) method of Collections, which sorts the specified list according to the order generated by the specified comparator. But there is a prerequisite, that is, all elements must be compared according to the provided comparator, as follows:
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; public class TreeMapTest { public static void main(String[] args) { Map<String, String> map = new TreeMap<String, String>(); map.put("a", "ddddd"); map.put("c", "bbbbb"); map.put("d", "aaaaa"); map.put("b", "ccccc"); //这里将map.entrySet()转换成list List<Map.Entry<String,String>> list = new ArrayList<Map.Entry<String,String>>(map.entrySet()); //然后通过比较器来实现排序 Collections.sort(list,new Comparator<Map.Entry<String,String>>() { //升序排序 public int compare(Entry<String, String> o1, Entry<String, String> o2) { return o1.getValue().compareTo(o2.getValue()); } }); for(Map.Entry<String,String> mapping:list){ System.out.println(mapping.getKey()+":"+mapping.getValue()); } } }
The running results are as follows:
d:aaaaa c:bbbbb b:ccccc a:ddddd
The above Java Map implementation method of sorting by key and sorting by value is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website.
For more Java Map implementation methods of sorting by key and sorting by Value, please pay attention to the PHP Chinese website!