Sunday, December 1, 2019

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 this method -
    Map m = Collections.synchronizedMap(hashMap);
    
  • HashMap allows one null value as a key and any number of null values where as 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();){
               System.out.println(e.nextElement());
               numbers.put("four", 4);
           }
           
           // Using iterator
           Iterator<String> itr =  numbers.keySet().iterator();
           while (itr.hasNext()){
               System.out.println(numbers.get(itr.next()));
               numbers.put("five", 5);
           }  
        }
    }
    

    Output

    two
    one
    three
    four
    2
    Exception in thread "main" java.util.ConcurrentModificationException
     at java.util.Hashtable$Enumerator.next(Unknown Source)
     at org.netjs.prog.HashTableDemo.main(HashTableDemo.java:22)
    

    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 Internally Works in Java
  2. LinkedHashMap in Java
  3. TreeMap in Java
  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
  4. Setter-based dependency injection in Spring
  5. How to remove duplicate elements from an ArrayList in Java
  6. EnumSet in Java
  7. BlockingQueue in Java Concurrency
  8. Functional interface annotation in Java 8