The difference between map and Hashmap,hashtable,hashset in Java

Source: Internet
Author: User

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

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.