Difference between HashMap and Hashtable in java

Difference between HashMap and Hashtable in Java question oftenly asked in core Java interviews to check whether candidate understand correct usage of collection classes and aware of alternative solutions available.

Before seeing differences between HashMap and Hashtable, let's see some similarities between these two and why we can use HashMap in place of Hashtable on certain scenario.

Similarities between HashMap and Hashtable

  1. Both Hashtable and HashMap implements java.util.Map interface.
  2. Hashtable and HashMap both are hash based collection and works on principle of hashing.
  3. Hashtable and HashMap both provide constant time performance for put and get method if objects are distributed uniformly across bucket.
  4. Both HashMap and Hashtable does not guarantee that the order of the map will remain constant over time. Instead use LinkedHashMap, as the order remains constant over time.
  5. From JDK 4 both Hashtable and HashMap are part of Java collection framework.

Difference between HashMap and Hashtable


Now let's see some key difference between HashMap and Hashtable in Java, this will decide when is the right time to use Hashtable over HashMap and vice-versa. Differences are based upon properties like synchronization, thread safety, speed, performance , Fail fast and Null key etc.

Synchronization and Thread-Safetey


One of the major differences between HashMap and Hashtable is that HashMap is non synchronized whereas Hashtable is synchronized, which means Hashtable is thread-safe and can be shared between multiple threads but HashMap can not be shared between multiple threads without proper synchronization. Java 5 introduces ConcurrentHashMap which is an alternative of Hashtable and provides better scalability than Hashtable in Java.

Null keys and null values


Hashmap allows one null key and any number of null values, while Hashtable do not allow null keys and null values in the HashTable object.

Performance


Since HashMap is not synchronized it perform better than Hashtable.

fail-fast


First let's understand what is fail-fast? If the collection (ArrayList, vector etc) gets structurally modified by any means, except the add or remove methods of iterator, after creation of iterator then the iterator will throw ConcurrentModificationException. Structural modification refers to the addition or deletion of elements from the collection.

The iterator in Hashmap is fail-fast iterator while the enumerator for Hashtable is not.

Super Class and Legacy


Hashtable is a subclass of Dictionary class which is now obsolete in Jdk 1.7 ,so ,it is not used anymore. It is better off externally synchronizing a HashMap or using a ConcurrentMap implementation (e.g ConcurrentHashMap).HashMap is the subclass of the AbstractMap class. Although Hashtable and HashMap has different superclasses but they both are implementations of the "Map" abstract data type.

How to make HashMap synchronized?


HashMap can be synchronized using:
Map map=Collections.synchonizedMap(hashmap) ;

When to use HashMap and when to use Hashtable?

  1. Single Threaded Application
  2. HashMap should be preferred over Hashtable for the non-threaded applications. In simple words , use HashMap in unsynchronized or single threaded applications .
  3. Multi Threaded Application
  4. We should avoid using Hashtable, as the class is now obsolete in latest Jdk 1.8 . Oracle has provided a better replacement of Hashtable named ConcurrentHashMap. For multithreaded application prefer ConcurrentHashMap instead of Hashtable.
Difference between HashMap and Hashtable

Hope we are able to explain you Difference between HashMap and Hashtable, if you have any questions or suggestions please write to us using contact us form.(Second Menu from top left).

Please share us on social media if you like the tutorial.
SHARE
    Blogger Comment
    Facebook Comment