About NewTechnoBuzz
Contact Us

Friday, July 18, 2014

Difference between HashMap and HashTable

Hashtable and HashMap are two hash based collection in Java and used to store objects as key value pair. Despite being hash based and similar in functionality there are significant difference between Hashtable and HashMap.

This question is oftenly asked in core Java interviews to check whether candidate understand correct usage of collection classes and aware of alternative solutions available along with How HashMap internally works in Java.

In this article, we will not only see some important differences between HashMap and Hashtable along with similarities between these two collection classes.

Similarities between Hashtable and HashMap

There are lot of similarities between Hashtable and HashMap in Java which is good to know and these also helps to find exactly what is different between HashMap and Hashtable in Java:
  • Both Hashtable and HashMap implements java.util.Map interface.
  • Both are hash based collection and works on principle of hashing.
  • Both provide constant time performance for put and get method if objects are distributed uniformly across bucket.
We have seen the similarities between HashMap and HashTable. Now, we'll see how they differ from each other. Below are some key differences:
  • Synchronization or Thread Safe - 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 cannot be shared between multiple threads without proper synchronization. Java 5 introduced 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.
  • Iterating the values - The third significant difference between HashMap vs Hashtable is that Iterator in the HashMap is a fail-fast iterator while the enumerator for the Hashtable is not and throw ConcurrentModificationException if any other Thread modifies the map structurally by adding or removing any element except Iterator’s own remove() method. But this is not a guaranteed behavior and will be done by JVM on best effort. This is also an important difference between Enumeration and Iterator in Java.
  • Performance - Hashmap is much faster and uses less memory than Hashtable as former is unsynchronized . Unsynchronized objects are often much better in performance in compare to synchronized object like Hashtable in single threaded environment.
  • Superclass 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.

Example of HashMap and HashTable

import java.util.Hashtable;
import java.util.HashMap;

public class TestHashMapHashtableClass {
    public static void main(String[] args) { 

      Hashtable<String,String> table = new Hashtable<String, String>();
      table.put("India", "New Delhi");
      System.out.println("Hashtable output :"+ table);

      HashMap hashMap = new HashMap();
      hashMap.put("India", "New Delhi");  
      System.out.println("HashMap output :" + hashMap);

When to use HashMap and Hashtable?

  • Single Threaded Application - HashMap should be preferred over Hashtable for the non-threaded applications. In simple words ,use HashMap in unsynchronized or single threaded applications .
  • Multi Threaded Application - We should avoid using Hashtable, as the class is now obsolete. Oracle has provided a better replacement of Hashtable named ConcurrentHashMap. For multithreaded, application prefer ConcurrentHashMap instead of Hashtable.
This is the information that I have right now. Please give your valuable comments, suggestions and feedback to improve this article.