Differences between Map and HashMap, Hashtable, and HashSet in Java, hashmaphashset

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.