Hashmap hashtable linkedhashmap and treemap
Java defines an interface java. util. Map for ing in the data structure. It has four implementation classes: hashmap hashtable linkedhashmap and treemap.
MAP is mainly used to store key-value pairs and obtain values based on keys. Therefore, duplicate keys are not allowed, but repeated values are allowed.
Hashmap is the most commonly used map. It stores data based on the hashcode value of the key, and can directly obtain its value based on the key, with fast access speed. Hashmap allows a maximum of null keys for one record, and null values for multiple records. hashmap does not support thread synchronization, that is, multiple threads can write hashmap simultaneously at any time; data inconsistency may occur. If synchronization is required, use the synchronizedmap method of collections to synchronize hashmap.
Hashtable is similar to hashmap. The difference is that it does not allow record keys or empty values; it supports thread synchronization, that is, only one thread can write hashtable at any time, as a result, hashtale writes slowly.
Linkedhashmap stores the record insertion sequence. When iterator is used to traverse linkedhashmap, the first record must be inserted first. It will be slower than hashmap during traversal.
Treemap can sort the records stored by the key. By default, the record is sorted in ascending order. You can also specify a comparator. When you use iterator to traverse the treemap, the obtained records are sorted in ascending order.
Demo
Package COM. wei. tools; import Java. util. hashmap; import Java. util. hashtable; import Java. util. iterator; import Java. util. linkedhashmap; import Java. util. map; import Java. util. treemap; import Java. util. map. entry; import Org. JUnit. test; public class T6 {// unordered @ testpublic void thashmap () {Map <string, string> map = new hashmap <string, string> (); map. put ("A", "qwer"); map. put ("B", "ASDF"); map. put ("C", "zxcv"); map. put ("D", "tyui"); map. put (null, null); iterator <entry <string, string> it = map. entryset (). iterator (); While (it. hasnext () {map. entry <string, string> entity = (Entry <string, string>) it. next (); system. out. println ("[Key =" + entity. getkey () + ", value =" + entity. getvalue () + "]");} system. out. println ();} // ordered @ testpublic void tlinkedhashmap () {Map <string, string> map = new linkedhashmap <string, string> (); map. put ("A", "qwer"); map. put ("B", "ASDF"); map. put ("C", "zxcv"); map. put ("D", "tyui"); iterator <entry <string, string> it = map. entryset (). iterator (); While (it. hasnext () {map. entry <string, string> entity = (Entry <string, string>) it. next (); system. out. println ("[Key =" + entity. getkey () + ", value =" + entity. getvalue () + "]");} system. out. println () ;}@ testpublic void thashtable () {Map <string, string> map = new hashtable <string, string> (); map. put ("A", "qwer"); map. put ("B", "ASDF"); map. put ("C", "zxcv"); map. put ("D", "tyui"); // The hashtable key values cannot be empty iterator <entry <string, string> it = map. entryset (). iterator (); While (it. hasnext () {map. entry <string, string> entity = (Entry <string, string>) it. next (); system. out. println ("[Key =" + entity. getkey () + ", value =" + entity. getvalue () + "]");} system. out. println ();} // natural sorting of treemap @ testpublic void ttreemap () {Map <string, string> map = new treemap <string, string> (); map. put ("B", "ASDF"); map. put ("A", "qwer"); map. put ("D", "tyui"); map. put ("C", "zxcv"); iterator <entry <string, string> it = map. entryset (). iterator (); While (it. hasnext () {map. entry <string, string> entity = (Entry <string, string>) it. next (); system. out. println ("[Key =" + entity. getkey () + ", value =" + entity. getvalue () + "]");} system. out. println ();}}
Differences between hashmap and hashtable: first, the hashmap thread is not synchronized (for fast access), and The hashtable thread is synchronized and can only be accessed by one thread at a time.
Second, hashmap allows null key values and hashtable does not.
Difference between hashmap and treemap: treemap uses the structure storage of the tree. treemap has more overhead than hashmap in operations, but can be sorted by keywords.
Treemap does not allow null values