Thursday, 12 November 2015

Difference between HashMap and HashTable in Java

Though both HashTable and HashMap store elements as a (key, value) pair and use hashing technique to store elements, also from Java v1.2, HashTable class was 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.

HashMap Vs HashTable

  • 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. fail-fast Vs fail-safe iterator in Java
  4. Treemap in Java
  5. How to loop through a map in Java
  6. Java Collections interview questions

You may also like -

1 comment:

  1. Well Described... visit Modern Pathshala to read about hashmap and hashtable

    ReplyDelete