Difference between HashMap and HashTable? Can we make hashmap synchronized? HashTable vs HashMap?

This question often asked in interview to check whether candidate understand correct usage of collection classes and aware of alternative solutions available.

  •  The HashMap class is roughly equivalent to Hashtable, except that it is non synchronized and permits nulls. (HashMap allows null values as key and value whereas Hashtable doesn’t allow nulls).
  • HashMap does not guarantee that the order of the map will remain constant over time.
  • HashMap is non synchronized whereas Hashtable is synchronized.
  • Iterator in the HashMap is  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.
Note on Some Important Terms
  1. Synchronized means only one thread can modify a hash table at one point of time. Basically, it means that any thread before performing an update on a hashtable will have to acquire a lock on the object while others will wait for lock to be released.
  2. Fail-safe is relevant from the context of iterators. If an iterator has been created on a collection object and some other thread tries to modify the collection object “structurally”, a concurrent modification exception wjavascript:void(0)ill be thrown. It is possible for other threads though to invoke “set” method since it doesn’t modify the collection “structurally”. However, if prior to calling “set”, the collection has been modified structurally, “IllegalArgumentException” will be thrown.
  3. Structurally modification means deleting or inserting element which could effectively change the structure of map.
    HashMap can be synchronized by
    Map m = Collections.synchronizeMap(hashMap);
Some more differences between hashtable and hashmap
  1. hashtable extends Dictionary interface which is quite old while hashmap extends Map interface.
  2. hashtalbe doesn’t have counterpart like ConcurrentHashMap.
  3. another important difference between hashtable and hashmap is , hashtable is less secure than hashmap because of Enumeration it uses. while hashmap uses iterator which prevents Concurrent Modification of HashMap, which is not possible in case of hashtable.
  4. stay out of hashtable use hashmap instead.


Hope this will be useful.

About Sanju
I am Software Programmer. I am working in JAVA/J2EE Technologies.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: