Thursday, 7 January 2016

Difference between HashMap and Hashtable

In my previous posts, I explained how HashMap works in Java, How Hashtable works inJava, Why get method is synchronized in Hashtable. Both HashMap and Hashtable are built on concept of hashing, following are some key differences between HashMap and Hashtable.

1. Hashtable is synchronized and thread safe, where s Hashmap is not. So it is better to use HashMap in Single threaded environment to achieve better performance.

2. Hashtable don't allow null keys and null values, where as HashMap allows on null key and any number of null values.

3. We can't predict the order of elements in both HashMap and Hashtable, But by using LinkedHashMap class which is a sub class of HashMap, we can expect the elements in the same way that we inserted. Go through the post, How LinkedHashMap worksin Java.


4. You can traverse HashMap using iterator, where as Hashtable provides both iterator and enumerator to traverse. Difference between iterator and enumerator explain the differences between iterator and enumerator.
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

public class HashMapVsTableDemo {
  public static void main(String ptr[]) {
    Map<Integer, String> hashMap = new HashMap<>();
    Hashtable<Integer, String> hashTable = new Hashtable<>();

    hashMap.put(1, "Hari Krishna");
    hashMap.put(2, "Jaideep");
    hashMap.put(3, "Sailaja");
    hashMap.put(4, "Sankalp");

    hashTable.put(1, "Hari Krishna");
    hashTable.put(2, "Jaideep");
    hashTable.put(3, "Sailaja");
    hashTable.put(4, "Sankalp");

    /* Traverse HashMap using iterator */
    System.out.println("Traverse HashMap using iterator");
    System.out.println("*******************************");
    Iterator<Map.Entry<Integer, String>> iter = hashMap.entrySet()
        .iterator();
    while (iter.hasNext()) {
      Map.Entry<Integer, String> pair = iter.next();
      System.out.println(pair.getKey() + " = " + pair.getValue());
    }

    /* Traverse Hashtable using iterator */
    System.out.println("Traverse Hashtable using iterator");
    System.out.println("*******************************");
    iter = hashTable.entrySet().iterator();
    while (iter.hasNext()) {
      Map.Entry<Integer, String> pair = iter.next();
      System.out.println(pair.getKey() + " = " + pair.getValue());
    }

    /* Traverse Hashtable using Enumerator */
    System.out.println("Traverse Hashtable using enumerator");
    System.out.println("*******************************");
    Enumeration<Integer> keys = hashTable.keys();
    while (keys.hasMoreElements()) {
      int key = keys.nextElement();
      System.out.println(key + " : " + hashTable.get(key));
    }
  }
}


Output

Traverse HashMap using iterator
*******************************
1 = Hari Krishna
2 = Jaideep
3 = Sailaja
4 = Sankalp
Traverse Hashtable using iterator
*******************************
4 = Sankalp
3 = Sailaja
2 = Jaideep
1 = Hari Krishna
Traverse Hashtable using enumerator
*******************************
4 : Sankalp
3 : Sailaja
2 : Jaideep
1 : Hari Krishna

5. The iterators returned by HashMap, Hashtable class are fail-fast. Enumerator in Hashtable is fail-safe.

6. Hashtable is a legacy class, introduced in JDK1.0, where as HashMap is part of Java collection framework introduced in Jdk 1.2.

Note

In multi threaded applications always prefer to use ConcurrentHashMap than Hashtable to achieve better performance.

                                                 Home

No comments:

Post a Comment