Saturday, December 24, 2022

Difference Between HashMap And Hashtable in Java

Though both Hashtable and HashMap in Java have some similarities like storing elements as a (key, value) pair and using hashing technique to store elements. From Java V1.2, Hashtable class was also retrofitted to implement the Map interface, making it a member of the Java Collections Framework. Still there are some differences in these two data structures. In this post we'll see those differences between HashMap and Hashtable in Java.

HashMap Vs Hashtable in Java

  • HashMap is not synchronized where as Hashtable is synchronized. Which essentially means that Hashtable is inherently thread safe where as HashMap is not. If we need to synchronize a HashMap then that has to be done explicitly by calling the following method.
    Map m = Collections.synchronizedMap(hashMap);
  • HashMap allows one null value as a key and any number of null values.
    Hashtable does not allow null values either as key or as value.
  • For traversing a HashMap an iterator can be used. That iterator is fail-fast and throws ConcurrentModificationException if any other Thread modifies the map structurally by adding or removing any element except Iterator's own remove() method.
    For traversing a Hashtable either an iterator or Enumerator can be used. Here again the iterator is fail-fast where as Enumerator is fail-safe.
    public class HashTableDemo {
      public static void main(String[] args) {
        Hashtable<String, Integer> numbers = new Hashtable<String, Integer>();
        numbers.put("one", 1);
        numbers.put("two", 2);
        numbers.put("three", 3);
        // Using enumerator
        for (Enumeration<String> e = numbers.keys(); e.hasMoreElements();){
          numbers.put("four", 4);
        // Using iterator
        Iterator<String> itr =  numbers.keySet().iterator();
        while (itr.hasNext()){
          numbers.put("five", 5);


    Exception in thread "main" java.util.ConcurrentModificationException
     at java.util.Hashtable$ Source)
     at org.netjs.prog.HashTableDemo.main(

    Here it can be seen that while enumerating a Hashtable if a new value is added (i.e. Hashtable is structurally modified) that doesn't throw any exception. Whereas, if a new value is added, while iterating it throws a ConcurrentModificationException.

  • Performance wise HashMap is faster than the Hashtable reason being HashMap is not synchronized.

That's all for this topic Difference Between HashMap And Hashtable in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

Related Topics

  1. How HashMap Works Internally in Java
  2. LinkedHashMap in Java With Examples
  3. TreeMap in Java With Examples
  4. Difference Between HashMap And ConcurrentHashMap in Java
  5. Java Collections Interview Questions And Answers

You may also like-

  1. static Reference to The Non-static Method or Field Error
  2. Abstraction in Java
  3. ThreadLocal class in Java With Examples
  4. Setter-based dependency injection in Spring
  5. How to Remove Duplicate Elements From an ArrayList in Java
  6. EnumSet in Java With Examples
  7. Java BlockingQueue With Examples
  8. @FunctionalInterface Annotation in Java

1 comment: