The 5 main differences betwen HashMap and Hashtable

HashMap and Hashtable both implement java.util.Map interface but there are some differences that Java developers must understand to write more efficient code. As of the Java 2 platform v1.2, Hashtable class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework.

  • 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.Synchronized means only one thread can modify a hash table at one point of time. Basically, it means that any thread before performing an update on a hashtable will have to acquire a lock on the object while others will wait for lock to be released.
  • The HashMap class is roughly equivalent to Hashtable, except that it permits nulls. (HashMap allows null values as key and value whereas Hashtable doesn’t allow nulls).
  • 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.
  • One more notable difference between Hashtable and HashMap is that because of thread-safety and synchronization Hashtable is much slower than HashMap if used in Single threaded environment. So if you don’t need synchronization and HashMap is only used by one thread, it out perform Hashtable in Java.
  • HashMap does not guarantee that the order of the map will remain constant over time.

Note that HashMap can be synchronized by

Map m = Collections.synchronizedMap(hashMap);

In Summary there are significant differences between Hashtable and HashMap in Java e.g. thread-safety and speed and based upon that only use Hashtable if you absolutely need thread-safety, if you are running Java 5 consider using ConcurrentHashMap in Java.

Cheers

  • Bapi

    nice explanation for choosing a HashMap and HashTable. using Java5 or above avoid using hashtable is the conclussion.

  • Arun

    Explanation is good

  • Hugo Bex

    Very good.

  • Kiran G

    Vert good explanation

  • Ravi

    nice Explanation and Now I know what to use and when 🙂

  • Paulo

    Good explanation, thank you.

  • Ashok

    nice one

  • Pratik

    nice .Thanks

  • Vikram N

    Manish,

    Nice one! This is usually favorite Java interview Q!

    • Vikram N

      ‘Synchronized means only one thread can modify a hash table at one point of time. Basically, it means that any thread before performing an update on a hashtable will have to acquire a lock on the object while others will wait for lock to be released’.

      ^
      Very well explained for newbies.

  • Martin

    Very nice summary 🙂

    I remember from the book Java Concurrency in Practice that the JVM is smart enough to ignore locks on synchronized object that are actually accessed by only one thread.

    That would mean that the 4th point (“Hashtable is much slower than HashMap if used in Single threaded environment”) would not hold.

  • satish

    Nice explanation.

    There are 5 basic differentiations with HashTable and HashMaps.

    1. Maps allows you to iterate and retrieve keys, values, and both key-value pairs as well, Where HashTable don’t have all this capability.
    2. In Hashtable there is a function contains(), which is very confusing to use. Because the meaning of contains is slightly deviating. Whether it means contains key or contains value? tough to understand. Same thing in Maps we have ContainsKey() and ContainsValue() functions, which are very easy to understand.
    3. In hashmap you can remove element while iterating, safely. where as it is not possible in hashtables.
    4. HashTables are by default synchronized, so it can be used with multiple threads easily. Where as HashMaps are not synchronized by default, so can be used with only single thread. But you can still convert HashMap to synchronized by using Collections util class’s synchronizedMap(Map m) function.
    5. HashTable won’t allow null keys or null values. Where as HashMap allows one null key, and multiple null values.

    • Vishwas

      Two things 1. HashTable does not has any contains method as mentioned in the second point.
      2. HashTable allows Null values but does not allow Null Keys

      • rajesh

        1)Hashtable is class that is not right way to write “Hash Table”
        2)Hashtable contains methods
        1)contains(); 2)containKey();3)containsValue();

  • Deepanshu Sehgal

    super explanation

  • keshav

    Does HashTable really extend Map interface???
    http://docs.oracle.com/javase/7/docs/api/java/util/Hashtable.html

    the above link does not show the same.

    • Manish

      Keshav,

      The link you shared clearly shows

      All Implemented Interfaces:
      Serializable, Cloneable, Map

      And therefore HashTable implements Map interface.

      Thanks
      Manish

      • rashid

        wrecked

  • Narasimha

    nice

  • anchal dhiman

    Very Nice … visit more Java Map Examples