1. Theoretical preparation
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 Red-Black tree, which is sorted in the natural order of its keys or based on the Comparator provided when the mapping is created, depending on the constructor used.
The values of HashMap 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.
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 can sort collection objects or arrays, and implement the public comparison(T o1,To2) method of this interface to realize the 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) { // Sort in descending order return obj2.compareTo(obj1); } }); map.put("b", "cccccc"); 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 operation results are as follows:
d:aaaaac:bbbbbb:ccccca:dddddd
3. Value sorting
The above example is to sort according to the key value of TreeMap, but sometimes we need to sort according to the value of TreeMap. To sort value, we need to use the sort(List<T> list, Comparator<? super T> 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 based on 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", "dddddd"); map.put("c", "bbbbbb"); map.put("d", "aaaaa"); map.put("b", "cccccc"); // Here convert map.entrySet() to list List<Map.Entry<String,String>> list = new ArrayList<Map.Entry<String,String>>(map.entrySet()); //Then use a comparator to implement sorting Collections.sort(list,new Comparator<Map.Entry<String,String>>() { //Sorting 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 operation results are as follows:
d:aaaaac:bbbbbb:ccccca:dddddd
The above Java Map sorting by key and value implementation method is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.