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, which 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. You can also specify the 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 treeMap = new TreeMap<>(new Comparator() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1; //Reverse order. Let me explain here that if a negative value is returned, o1 will output first, otherwise o2 will
}
});
//Fill in data
for(int i = 0; i < 100;i++){
int key = (int)(10000*());
int value = (int)(10000*());
(key, value);
}
outMap(treeMap);
}
public static void outMap(Map map){
for(Integer integer:()){
("key="+integer+" value="+(integer));
}
}
/* The results are as follows:
key=9977 value=80
key=9684 value=7108
key=9422 value=1706
key=9264 value=1210
key=9248 value=4758
key=9024 value=7048
key=8892 value=3124
key=8879 value=6414
key=8814 value=8171
key=8728 value=1538
key=8513 value=4956
key=8462 value=5617
key=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 a collection of Entry<>
Then use List> to implement sorting
Insert the sorted elements into LinkedMap
The code implementation is as follows:
private static Map sortMap(Map linkedMap) {
List> cache = new ArrayList<>(());
//Rewrite the comparison function
(cache,new Comparator>() {
@Override
public int compare(Entry o1, Entry o2) {
//If the return value is less than 0, then o1 is before o2
return ()-();
}
});
Map resultMap = new LinkedHashMap<>();
//Insert the result into LinkedMap and return
for(int i = 0; i < ();i++){
((i).getKey(), (i).getValue());
}
return resultMap;
}
/*result:
7965 9966
1067 9963
1720 9833
3257 9738
3934 9578
777 9348
1924 9315
3472 9270
3649 9114
5892 9078
*/
In this way, both sorting by value and sorting by key can be achieved.