HashMap、HashTable、ConcurrentHashMap区别

HashMap and HashTable What's the difference?

Security Thread:

The method of the HashTable are synchronized, and a method of HashMap default is asynchronous. In multi-threaded environment, you can directly use HashTable, but to use their own HashMap, then we must increase the synchronization process.

Inheritance: HashTable is based on obsolete Dictionary class inherited. HashMap AbstractMap inherited abstract class implements the Map interface.

It allows null values: HashTable in, key and value are not allowed null value, otherwise it will throw a NullPointerException. The HashMap, as null key, only one such bond; can have one or more keys corresponding to the value of null.

The default initial capacity and expansion mechanisms: hash HashTable initial size of the array is 11, it is a way to increase old * 2 + 1. The default size of the HashMap hash array 16, and must be a power of two. Reason for the entire network to a Map hash () The article analyzes the most thorough, no other two. -HollisChuang's Blog

Different hash values: HashTable directly hashCode object. HashMap recalculate the hash value.

Internal traversal to achieve different: Hashtable, HashMap use the Iterator. And for historical reasons, Hashtable also used the Enumeration way. HashMap implement Iterator, supports fast-fail, Hashtable of Iterator traversal support fast-fail, with Enumeration does not support fast-fail

HashMap and ConcurrentHashMap difference?

ConcurrentHashMap and HashMap implementation is not the same, though they are using a bucket array to achieve, but there are differences, ConcurrentHashMap array of segmented barrel, while HashMap is not.

ConcurrentHashMap in each segment have been protected by a lock. HashMap no lock mechanism. Therefore, the former security thread, which is not thread safe.

PS: the above differences based on jdk1.8 previous version.

Guess you like

Origin www.cnblogs.com/lujiahua/p/11408810.html