Tuesday, June 9, 2020

Differences between HashMap and Hashtable


Both HashMap and Hashtable classes are Map interface implementations . HashMap and Hashtable classes have below defferences based on their functionality and behavior.

HashMapHashtable
HashMap is not a legacy classHashtable is a legacy class
HashMap is not synchronized. If required HashMap can be synchronized as below:
Map m = Collections.synchronizeMap(hashMap);
Hashtable is internally synchronized and can't be unsynchronized. It is thread-safe and can be shared with many threads.
HashMap can contain one null key and multiple null values.Hashtable cannot contain any null key or null value.
HashMap is not thread-safe, so it is useful for non-threaded applications.Hashtable is thread-safe, and it can be shared between various threads.
HashMap inherits the AbstractMap classHashtable inherits the Dictionary class.
Iterator in the HashMap is fail-safeEnumerator for the Hashtable is not and throw ConcurrentModificationException
HashMap is fastHashtable is slow
HashMap inherits AbstractMap class.Hashtable inherits Dictionary class.
HashMap is traversed by Iterator.Hashtable is traversed by Enumerator and Iterator.

No comments:

Post a Comment

Hello Buddy, if you have any doubts or need any clarification , feel free to comment. Thanks.