The HashMap and Hashtable two classes implement the map interface, which holds the k-v pair (Key-value pair), and HashSet implements the set interface, which is similar in nature to a collection.
Hashtable and HashMap differences
first, the inherited parent class is differentHashtable inherits from the dictionary class, and HashMap inherits from the Abstractmap class. But both implement the map interface.
second, thread security is differentThe methods in Hashtable are synchronize, and the methods in HashMap are non-synchronize by default. In the multi-threaded concurrency environment, you can use Hashtable directly, do not need to implement its own method of synchronization, but the use of hashmap you have to increase the synchronization process.
third, whether to provide contains methodHashMap hashtable contains method removed, changed to Containsvalue and ContainsKey, because the contains method is easy to cause misunderstanding.
Hashtable retains the Contains,containsvalue and ContainsKey three methods, where contains and Containsvalue function the same.
Iv. whether the key and value allow null valueswhere key and value are objects and cannot contain duplicate keys, but can contain duplicate value.
In Hashtable, both key and value do not allow null values.
In HashMap, NULL can be used as a key with only one key, and one or more keys can have a value of NULL. When the Get () method returns a null value, the key may not be in HashMap, or the value corresponding to the key may be null. Therefore, the get () method cannot be used in HashMap to determine whether a key exists in HashMap and should be judged by the ContainsKey () method.
Five, two traversal methods of the internal implementation of the differentHashtable and HashMap all use the Iterator. For historical reasons, Hashtable also used the enumeration approach.
Six, the hash value is differenthash values are used differently, Hashtable directly using the object's hashcode. The hash value is recalculated by the HashMap.
Vii. internal implementations use array initialization and expansion in different waysHashtable and hashmap their two internal implementations of the array's initial size and the way the capacity is expanded. The default size of the hash array in 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.
The difference between map and Hashmap,hashtable,hashset in Java