Types of Map
In Java, the main function of Map is to store key-value pairs. Since the value is obtained based on the key, the key is not allowed to be repeated. It mainly has the following categories:
HashMap:
The most commonly used Map stores data according to the HashCode value of the key, and can directly obtain its value according to the key, with a very fast access speed. When traversing, the order of data acquisition is completely random. HashMap allows only one record to be Null; the value of multiple records to be Null; HashMap does not support thread synchronization, that is, multiple threads can write HashMap at any time; it may lead to inconsistency in data. If synchronization is required, you can use the synchronizedMap method of Collections to make HashMap synchronized, or use ConcurrentHashMap. Hashtable is similar to HashMap. It inherits from the Dictionary class. The difference is that it does not allow the recorded keys or values to be empty; it supports thread synchronization, that is, only one thread can write Hashtable at any time, so it also causes Hashtable to be slower when writing.
LinkedHashMap
The insertion order of records is saved. When traversing LinkedHashMap with Iterator, the records obtained first must be inserted first. You can also use parameters during construction and sort them according to the number of applications. It will be slower than HashMap when traversing, but there is an exception. When HashMap has a large capacity and is less actual data, it may be slower than LinkedHashMap, because the traversal speed of LinkedHashMap is only related to the actual data and has nothing to do with the capacity, while HashMap's traversal speed is related to its capacity.
TreeMap
Implementing the SortMap interface, the records it saves can be sorted according to the key. By default, it is sorted in ascending order of key values, or it can also specify a sorted comparator. When it is traversed with Iterator, the records obtained are sorted.
Key sorting
From the above introduction to the types of maps, we can see that TreeMap has its own key sorting function. It only needs to implement a Compare interface at the same time when creating it. The example is as follows:
private static void sort_by_key(){ Map<Integer, Integer> treeMap = new TreeMap<>(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2-o1; //Inverse order. Let me explain here that if a negative value is returned, o1 will output first, otherwise o2 } }); //Fill the data for(int i = 0; i < 100;i++){ int key = (int)(10000*Math.random()); int value = (int)(10000*Math.random()); treeMap.put(key, value); } outMap(treeMap); } public static void outMap(Map<Integer, Integer> map){ for(Integer integer:map.keySet()){ System.out.println("key="+integer+" value="+map.get(integer)); }}/* The result is as follows: key=9977 value=80key=9684 value=7108key=9422 value=1706key=9264 value=1210key=9248 value=4758key=9024 value=7048key=8892 value=3124key=8879 value=6414key=8814 value=8171key=8728 value=1538key=8513 value=4956key=8462 value=5617key=8355 value=8912*/As can be seen from the above, it is not difficult to sort by keys, but it is more troublesome to place the sort, so you need to turn the map.
Sort by value
Since Map does not have this function in Java, we need to implement it ourselves. The idea is as follows:
List in Java can use the compare interface.
Map is actually an Entry<> collection. So using List<Entry<>> can implement sorting and inserting the sorted elements into LinkedMap.
The code implementation is as follows:
private static Map<Integer, Integer> sortMap(Map<Integer, Integer> linkedMap) { List<Map.Entry<Integer, Integer>> cache = new ArrayList<>(linkedMap.entrySet()); //Rewrite the comparison function Collections.sort(cache,new Comparator<Map.Entry<Integer, Integer>>() { @Override public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) { //If the return value is less than 0, then o1 is before o2 return o2.getValue()-o1.getValue(); } }); Map<Integer, Integer> resultMap = new LinkedHashMap<>(); //Insert the result into LinkedMap and return for(int i = 0; i < cache.size();i++){ resultMap.put(cache.get(i).getKey(), cache.get(i).getValue()); } return resultMap; }/*Result: 7965 99661067 99631720 98333257 97383934 9578777 93481924 93153472 92703649 91145892 9078*/In this way, both sorting by value and sorting by key can be achieved.