Differences between Map and HashMap, Hashtable, and HashSet in Java, hashmaphashset
HashMap and Hashtable both implement the Map interface, both save the K-V pair (key-value Pair); HashSet implements the Set interface, the nature is similar to the Set.
Differences between HashTable and HashMap
1. Different Hashtable inherited from the Dictionary class and HashMap inherited from the AbstractMap class. Both implement the Map interface.
II. The thread security is different. The methods in Hashtable are Synchronize, while the methods in HashMap are non-Synchronize by default. In a multi-threaded and concurrent environment, you can directly use Hashtable without having to implement synchronization for its own method, but you must add synchronization processing yourself when using HashMap.
Iii. Whether the contains method HashMap is provided to remove the contains method of Hashtable and change it to containsValue and containsKey, because the contains method is easy to be misunderstood.
Hashtable retains the contains, containsValue, and containsKey methods. The functions of contains and containsValue are the same.
4. Whether key and value allow null values. Both key and value are objects and cannot contain duplicate keys, but can contain duplicate values.
In Hashtable, keys and values cannot contain null values.
In HashMap, null can be used as a key. There is only one such key. The value corresponding to one or more keys is null. When the get () method returns a null value, it may be because the key does not exist in HashMap, or the corresponding value of the key may be null. Therefore, the get () method cannot be used to determine whether a key exists in the HashMap. Instead, the containsKey () method should be used to determine whether a key exists in the HashMap.
5. Iterator is used for different Hashtable and HashMap internal implementations of the two traversal methods. For historical reasons, Hashtable also uses the Enumeration method.
6. Use different hash values. HashTable directly uses the hashCode of the object. HashMap recalculates the hash value.
7. The array initialization and expansion modes used by the internal implementation are different. Hashtable and HashMap are two internal implementation methods: the initial size of the array and the expansion method. In HashTable, the default size of the hash array is 11, and the increment is old * 2 + 1.
The default size of the hash array in HashMap is 16, and it must be an index of 2.