map is a collection interface of key-value pairs. 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, HashMap values are not in order Yes, it is implemented according to the HashCode of the key, that is, the data is stored according to the HashCode value of the key. Its Value can be directly obtained according to the key, and it has a very 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 Iterator to traverse TreeMap, the records obtained are sorted. Out of order. TreeMap does not allow the key value to be null. Asynchronous.
Hashtable:Similar to HashMap, except that neither key nor value is allowed to be null; it supports thread synchronization, that is, only one thread can write to Hashtable at any time. Therefore, Hashtale will be slower when writing. Only hashtable inherits from the Dictionary abstract class, hashMap and treeMap both inherit from the AbstractMap abstract class, and LinkedHashMap inherits from hashMap.
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.
Common sense:
Do Collection and Map collections inherit from Object?
No, both are interfaces, and Object is a class. How could it be inherited from Object? Please see the specific interface under java.util for details.
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) { Mapmap = new TreeMap ( new Comparator () { 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 keySet = map.keySet(); Iterator iter = keySet.iterator(); while (iter.hasNext()) { String key = iter.next(); System.out.println(key + ":" + map.get(key)); } } }
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 able to be compared according to the provided comparator. As follows:
public class TreeMapTest { public static void main(String[] args) { Mapmap = new TreeMap (); map.put("d", "ddddd"); map.put("b", "bbbbb"); map.put("a", "aaaaa"); map.put("c", "ccccc"); //这里将map.entrySet()转换成list List > list = new ArrayList >(map.entrySet()); //然后通过比较器来实现排序 Collections.sort(list,new Comparator >() { //升序排序 public int compare(Entry o1, Entry o2) { return o1.getValue().compareTo(o2.getValue()); } }); for(Map.Entry mapping:list){ System.out.println(mapping.getKey()+":"+mapping.getValue()); } } }
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) { Mapmap = new HashMap (); map.put("c", "ccccc"); map.put("a", "aaaaa"); map.put("b", "bbbbb"); map.put("d", "ddddd"); List > list = new ArrayList >(map.entrySet()); Collections.sort(list,new Comparator >() { //升序排序 public int compare(Entry o1, Entry o2) { return o1.getValue().compareTo(o2.getValue()); } }); for(Map.Entry mapping:list){ System.out.println(mapping.getKey()+":"+mapping.getValue()); } } }
The above is the detailed content of 4 main map implementation classes. For more information, please follow other related articles on the PHP Chinese website!