Just beginning to see HASHTABLE,HASHMAP and TreeMap when more dizzy, feel the role of almost, but to the actual use of time and found there are many differences. So I searched some relevant information to learn, the following is my study precipitation.
Java defines an interface java.util.Map for the mappings in the data structure, and HashMap Hashtable and TREEMAP are its implementation classes. A map is an object that maps keys to values, a map cannot contain duplicate keys, and each key can only map one value at a maximum.
Hashmap is the most commonly used map, which stores data according to the hashcode value of the key, which can be obtained directly from the key, with fast access speed. 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.
Hashtable is similar to HashMap, but has a difference of 6 points.
1.HashTable method is synchronous, HashMap is not synchronized, so in multi-threaded occasions to manually synchronize HashMap this difference is like vector and ArrayList.
2.HashTable does not allow null values, neither key nor value can, HASHMAP allows null values, both key and value. HashMap allows the key value to be used only by a null value, because HASHMAP if the key value is the same, the new key, value will replace the old one.
3.HashTable has a contains (object value) feature and Containsvalue (object value) functionality.
4.HashTable use Enumeration,hashmap with iterator.
The default size of the hash array in 5.HashTable is 11, and the increment is old*2+1. The default size of the hash array in HashMap is 16, and must be a 2 index.
6. Using different hash values, Hashtable directly uses the object's hashcode.
TreeMap is able to sort its saved records by key, by default in ascending order, or by specifying a sort comparer, and when traversing treemap with iterator, the resulting records are sorted out.
Here is a classic example of Hashtable,hashmap and TreeMap summary.
Packagecom.taobao.luxiaoting; ImportJava.util.Map; ImportJava.util.HashMap; ImportJava.util.Set; ImportJava.util.HashSet; ImportJava.util.Iterator; Importjava.util.Hashtable; ImportJava.util.TreeMap; classHashmaps { Public Static voidMain (string[] args) {map map=NewHashMap (); Map.put ("A", "AAA"); Map.put ("B", "BBB"); Map.put ("C", "CCC"); Map.put ("D", "ddd"); Iterator Iterator=Map.keyset (). iterator (); while(Iterator.hasnext ()) {Object key=Iterator.next (); System.out.println ("Map.get (Key) is:"+Map.get (key)); } Hashtable Tab=NewHashtable (); Tab.put ("A", "AAA"); Tab.put ("B", "BBB"); Tab.put ("C", "CCC"); Tab.put ("D", "ddd"); Iterator iterator_1=Tab.keyset (). iterator (); while(Iterator_1.hasnext ()) {Object key=Iterator_1.next (); System.out.println ("Tab.get (Key) is:"+Tab.get (key)); } TREEMAP tmp=NewTreeMap (); Tmp.put ("A", "AAA"); Tmp.put ("B", "BBB"); Tmp.put ("C", "CCC"); Tmp.put ("D", "ddd"); Iterator iterator_2=Tmp.keyset (). iterator (); while(Iterator_2.hasnext ()) {Object key=Iterator_2.next (); System.out.println ("Tmp.get (Key) is:"+Tmp.get (key)); } } }
the output is as shown
This makes it clear that only the records obtained by TreeMap are ordered.
Some thoughts on hashtable,hashmap and TreeMap