1.

HashMap vs Hashtable in Java

Answer»
  • HASHMAP in Java is a part of the collection class in the java.util package. It is based on Maps and is used for storage of data in Key and Value pairs.

HashMap extends the AbstractMap class and ALSO implements the Cloneable and Serializable interface.

  • Hashtable in Java was an original part of the java.util package and was implementation of a Dictionary. It was updated by JDK 1.2 and implemented the Map Interface. Currently, it is a part of the collections framework. It also stores data in Key and Value pairs.

Despite of the similarities, HashMap and HashTable have few differences. Let us have a look at them.

BasisHashMapHashtable
Null keys/valuesNull key/values are allowed in HashMap. It allows one null key and MULTIPLE null values.Null key/values are not allowed in Hashtable
SynchronizationHashMap is not synchronized and is not thread safe. It can’t be shared amongst multiple threads without appropriate synchronization.Hashtable is synchronized and thread safe.
Nature of iteratorHashMap iterator is FAIL FAST in nature.Hashtable iterator is fail safe in nature.
IntroductionHashMap was introduced in JDK 1.2Hashtable is a legacy class.
PerformanceHashMap is fast.Hashtable is slower than HashMap
Parent ClassAbstractMap is the parent class for HashMapDictionary is the parent class for Hashtable


Discussion

No Comment Found