0
Sponsored Links


Ad by Google
Very very popular java interview question, you can say this is ever green java interview question and also one of my favorite java interview question like ArrayList vs LinkedList.

Sometimes interviewer just want to know What is the differences between HashMap and Hashtable and he/she is not asking you to explain how HashMap or Hashtable works, So you just need explain those difference points only. In this post we are going to list down the differences between HashMap and Hashtable. In our previous post we have explain Why String is immutable.

Well HashMap and Hashtable both are the implementation of Map interface and of course they are in java.util package. Although both are the implementation of java.util.Map interface, but still both have some differences and those are listed below.

Key differences between HashMap and Hashtable

  1. Hashtable is synchronized, whereas HashMap is not. Means working on HashMap is very very much fast as compared to Hashtable in single threaded application.
  2. Hashtable does not allow null keys or null values, whereas HashMap allows one null key and any number of null values.
  3. In HashMap possible to maintain insertion order by using one of the sub-class of HashMap known as LinkedHashMap, but this is not possible with Hashtable.
  4. Iterator in the HashMap is a fail-fast while the enumerator for the Hashtable is not and throw ConcurrentModificationException if any other thread modifies the map structurally by adding or removing any element except Iterator’s own remove() method. But this is not a guaranteed behavior and will be done by JVM on best effort.
  5. In HashMap default initial capacity is 16 elements, whereas Hashtable default initial capacity is of 11 elements.

Note: HashMap is not synchronized, but you can make HashMap synchronized at the creation time using the below statement.
Map mMap = Collections.synchronizedMap(new HashMap());

Sponsored Links

0 comments:

Post a Comment