In previous article, I have mentioned Servlet in Java with example (Saving form data in database using servlet) and How to compare Strings in Java? but in this article, I have provided detailed difference between popular collections of java that is Hashmap and Hashtable, but before going through difference, let's take understand each of them first.

Brief Introduction of HashMap and HashTable

HashMap and Hashtable store key/value pairs in a hash table. When using a Hashtable or HashMap, we specify an object that is used as a key, and the value that you want linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table.

Despite being hash based and similar in functionality there are a significant difference between Hashtable and HashMap and without understanding those difference if you use Hashtable in place of HashMap than you may run into series of subtle programs which is hard to find and debug.

Now, What is the difference between HasMap and HashTable, this is one of the common questions in Interview for Java/J2EE developers, so here i am going to explain you about it to make your concepts about it clear.

Understanding Hashmap and Hashtable one by one (with Difference)

Let's discuss each of them one by one:

HashMap

Java HashMap is one of the most popular Collection classes in java. Java HashMap is Hashtable based implementation. HashMap in java extends AbstractMap class that implements Map interface.

  1. HashMap is non-synchronized. So it can be used in the multi-threaded environment, ie., more than one thread can access and use it simultaneously.
  2. HashMap allows null values as key and value
  3. HashMap is generally preferred over HashTable if thread synchronization is not needed.
  4. Iterator in the HashMap is fail-fast 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.
  5. Example:
import java.util.*;  
class TestingCollection{  
 public static void main(String args[]){  
  HashMap<Integer,String> hm=new HashMap<Integer,String>();  
  hm.put(100,"Raj");  
  hm.put(101,"Manveer");  
  hm.put(102,"Vikram");  
  for(Map.Entry m:hm.entrySet()){  
   System.out.println(m.getKey()+" "+m.getValue());  
  }  
 }  
} 

Output of the above code will be

102 Raj
100 Manveer
101 Vikram

HashTable

Java Hashtable class implements a hashtable, which maps keys to values. It inherits Dictionary class and implements the Map interface.A Hashtable is an array of a list, it contains values based on the key & contains only unique elements.

  1. HashTable is thread safe and synchronized, it should be used in multithreading applications. 
  2. It doesn't allow null keys and null values in the HashTable object.
  3. Preferred when thread synchronization is needed.
  4. HashTable is the only class other than vector which uses the enumerator to iterate the values of HashTable object, the enumerations returned by the Hashtable keys and elements methods are not fail fast.
  5. Example:

    import java.util.*;  
    class TestingCollectionHashTable{  
     public static void main(String args[]){  
        Hashtable<Integer,String> ht=new Hashtable<Integer,String>();
            ht.put(101,"Amit");
            ht.put(101,"Aman");
            ht.put(102,"Vikram");
            ht.put(103,"Vinay");
           
            for (Map.Entry m:ht.entrySet()) {
                System.out.println(m.getKey()+" "+m.getValue());
            }
     }  
    } ?

    Output:

    103 Vinay
    102 Vikram
    101 Aman?

Quick Overview of difference between HashMap & HashTable

  1. The HashMap class is roughly equivalent to Hashtable, except that it is not synchronized and permits nulls. (HashMap allows null values as key and value whereas Hashtable doesn't allow nulls).
  2. HashMap does not guarantee that the order of the map will remain constant over time.
  3. HashMap is not synchronized whereas Hashtable is synchronized.
  4. Iterator in the HashMap is fail-safe 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.
  5. Hashtable is considered as legacy class while HashMap was introduced in JDK 1.2.
  6. HashMap inherits AbstractMap class while Hashtable inherits Dictionary class.
  7. HashMap is traversed by Iterator while Hashtable is traversed by Enumerator and Iterator.
  8. HashMap is fast, while Hashtable is slow

difference-between-hashmap-hashtable-in-java