When doing statistics today, you need to sort the areas on the X-axis according to the area code (areaCode). Since the map used in constructing XMLData is used for data statistics, the map needs to be sorted during the statistical process.
1. Brief introduction to Map
Before explaining Map sorting, let’s first understand a little about map. map is a collection interface of key-value pairs, and its implementation classes mainly include: HashMap, TreeMap, Hashtable and LinkedHashMap, etc. The differences between the four are as follows (brief introduction):
HashMap: Our most commonly used Map, it stores data according to the HashCode value of the key. Its Value can be obtained directly according to the key, and it has fast access speed. HashMap only allows the key value of one record to be Null (multiple records will be overwritten); it allows the Value of multiple records to be Null. Asynchronous.
TreeMap: It can sort the records it saves according to key. The default is in ascending order. You can also specify a sorting comparator. When using an Iterator to traverse the TreeMap, the records obtained are sorted. TreeMap does not allow the key value to be null. Asynchronous.
Hashtable: Similar to HashMap, except that the key and value values are not allowed to be null; it supports thread synchronization, that is, only one thread can write Hashtable at any time, which also leads to Hashtale in Writing will be slower.
LinkedHashMap: Saves the insertion order of records. When using Iterator to traverse LinkedHashMap, the record obtained first must be inserted first. It will be slower than HashMap during traversal. Both key and value are allowed to be empty and asynchronous.
2. Map sorting
TreeMap
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. This method is mainly based on the first parameter o1, which is less than , equal to or greater than o2 returns a negative integer, 0 or a positive integer respectively. As follows:
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("c", "ccccc"); map.put("a", "aaaaa"); map.put("b", "bbbbb"); map.put("d", "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:ddddd
c:ccccc
b:bbbbb
a:aaaaa
The above example is correct Sorting is based on the key value of the TreeMap, but sometimes we need to sort based on the value of the 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 able to be compared according to the provided comparator. As follows:
public class TreeMapTest { public static void main(String[] args) { Map<String, String> map = new TreeMap<String, String>(); map.put("d", "ddddd"); map.put("b", "bbbbb"); map.put("a", "aaaaa"); map.put("c", "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()); } } }
Run result
a:aaaaa
b:bbbbb
c:ccccc
d:ddddd
HashMap
We all have HashMap values that are not in order. They are implemented according to the HashCode of the key. How do we implement sorting for this unordered HashMap? Referring to the value sorting of TreeMap, we can also implement the sorting of HashMap.
public class HashMapTest { public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("c", "ccccc"); map.put("a", "aaaaa"); map.put("b", "bbbbb"); map.put("d", "ddddd"); 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()); } } }
Run results
a:aaaaa
b:bbbbb
c:ccccc
d:ddddd
More java how to sort map For detailed explanation (use of map collection) related articles, please pay attention to the PHP Chinese website!