Friday, September 27, 2013

HashMap Vs HashTable


HashMap
HashTable
HashMap & HashTable
Implements Map interface
Implements Map interface
Both Implements  Map interface
HashMap is not  Synchronized.

HashTable is Synchronized .


Not thread-safe, need to implement proper synchronization for multithreading.
In Java 5 we have ConcurrentHashMap, which is thread safe.
HashTable is Thread-safe, shared between multiple threads.

Faster than HashTable.
Better for non-threaded applications.
Much Slower than HashMap for single thread use.
(Synchronization makes HashTable slow)

HashMap allows null.
One  null Key and any number of  null values.
(so do the correct null check for HashMap. See the Null pointer exception in HashMap for details)
HashTable Doesn’t allow null keys or values.

Iterator in the HashMap is fail-fast iterator.


Enumerator in the HashTable is not fail-fast.

Doesn’t retain order.
Java.util.HashMap is unordered
Need to use  LinkedHashMap to maintain order.
Doesn’t retain order.
Both are using hash function  to store and retrieve values from the map.

HashMap is not Synchronized

HashMap is not synchronized. 
If  your code is executing in Single threaded environment it is better to use  HashMap.
But in the case of multi-threaded environment, your code will be shared among multiple threads.Here we need some way to ensure that the resource will be used  by only one thread at a time.Here we need synchronization of objects.
If your code is synchronized, it will executed by only one thread at a time.
In this case if your using HashMap, you need to do something to synchronize the HashMap.

Or you can use HashTable or ConcurrnetHashMap(From Java 5 onwards).

Synchronzing the HashMap
To synchronize HashMap we can use a method from collection API Collections.synchronizedMap().
  Map map = Collections.synchronizedMap(new HashMap());

HashMap is fail-fast

Fail-fast means when you try to modify the contents of HashMap when you are iterating through it, it will fail and throw ConcurrentModificationException.

       HashMap hm = new HashMap<String, String>();
        hm.put("A","1");
        hm.put("B","2");
        hm.put("C","3");
       String i;
       Set Keys = hm.keySet();
       for(Object key:Keys) {
              m.put("D", "4"); // it will throw ConcurrentModificationException
      
}
O/P:
Exception in thread "main" java.util.ConcurrentModificationException

To avoid this we have to use fail-safe iterator (ConcurrentHashMap)

HashTable is not fail-fast

HashTable it is using Enumeration for Keys access.

Enumerator is introduced in older version of java, so there is no way to remove items when ever we are accessing through enumerator.
For HashTable enumeration it will not throw ConcurrentModificationException exception

HashTable hm = new Hashtable<String, String>();
               hm.put("A","1");
               hm.put("B","2");
               hm.put("C","3");
              String i;
               for (Enumeration e = hm.elements() ; e.hasMoreElements() ; e.nextElement()) {
                     hm.put("D", "4"); // will not throw any exception

              }