Java defines an interface java.util.Map for the mappings in the data structure; it has four implementation classes, namely HashMap Hashtable Linkedhashmap and TreeMap
The map is primarily used to store the health-value pairs, and the key is not allowed to repeat (overwriting is repeated), but allows the values to be duplicated.
Hashmap is the most commonly used map, it stores data according to the hashcode value of the key, can get its value directly according to the key, has the fast access speed, when traversing, obtains the data the order is completely random. HashMap allows a maximum of one record's key to be null, allowing multiple records to have a value of NULL; HashMap does not support thread synchronization, where multiple threads can write HashMap at any one time, and may result in inconsistent data. If synchronization is required, you can use the collections Synchronizedmap method to make the HashMap capable of synchronizing or using Concurrenthashmap.
Hashtable is similar to HashMap, which inherits from the dictionary class, except that it does not allow the record key or value to be null; it supports thread synchronization, which means that only one thread can write hashtable at any one time, so it also causes the hashtable to be slower to write.
Linkedhashmap preserves the order in which records are inserted, and when traversing linkedhashmap with iterator, the first record must be inserted first. You can also use the parameters in the construction to sort by the number of applications. The traversal will be slower than HashMap, but there is an exception, when the HashMap capacity is large, the actual data is small, the traversal may be slower than Linkedhashmap, because the Linkedhashmap traverse speed is only related to the actual data, and capacity-independent, And HashMap's traverse speed is related to his capacity.
TreeMap implements the Sortmap interface, it can save the record according to the key sort, the default is the key value of ascending order, you can also specify a sort of comparator, when using iterator traversal treemap, the resulting records are ordered.
In general, we use the most is the hashmap,hashmap inside the key value of the pair when it is taken out is random, it according to the hashcode value of the key to store data, according to the key can directly get its value, with fast access speed. HashMap is the best choice for inserting, deleting, and locating elements in a map.
The treemap takes out the sorted key-value pairs. But if you want to traverse the key in natural order or in a custom order, TreeMap is better.
Linkedhashmap is a subclass of HashMap, if you need to output the same order and input, then with LINKEDHASHMAP can be implemented, it can also be arranged in the order of reading, like the connection pool can be applied.
The difference between Hashmap,linkedhashmap,treemap in Java