The difference between HashMap and Hashtable, can the key in HashMap be any object or data type? is hashtable thread-safe?

Source: Internet
Author: User
Tags hash thread

the difference between HashMap and Hashtable

1.HashTable method is synchronous, HashMap is not synchronized, so in multi-threaded occasions to do hands-on synchronization hashmap This difference is like vector and ArrayList.

2.HashTable does not allow null values (neither key nor value), HashMap allows null values (both key and value can be)

3. The way they traverse is much the same, Hashtable is only a elements way more than HashMap.

Both Hashtable and HashMap can return a collection through the values () method, and then traverse the process.

Both can also return a set through the EntrySet () method, and then perform traversal processing.

4.HashTable use Enumeration,hashmap with iterator.

5. Using different hash values, Hashtable directly uses the object's hashcode. The HashMap then recalculates the hash value and is used instead of the modulo.

The default size of the hash array in 6.HashTable is 11, and the increment is 2*old+1. The default size of the hash array in HashMap is 16, and must be a 2 index.

7.HashTable is based on the dictionary class, and HashMap is based on the Abstarctmap class.


the key in HashMap can be any object or data type.

1. can be null, but cannot be a mutable object, if it is a Mutable object, the object's properties change, then the object's hashcode also changed accordingly, so that the next time can not find the existing map data.

2. If a mutable object is used as a key in HashMap, be careful not to change its hash value when changing the state of the object. We only need to ensure that the change of the member variable guarantees that the hash value of the object will be constant.


Hashtable is thread-safe.

Hashtable is thread-safe, and its implementation is decorated with the addition of the Synchronized keyword on the corresponding method, which is slow to execute because the object lock needs to be obtained when executing this method. So now, if you want to keep the thread safe, use Concurrenthashmap.

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.