Saturday, October 2, 2010

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


Difference between HashMap and Hashtable



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

1. 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).
2. HashMap does not guarantee that the order of the map will remain constant over time.
3. HashMap is non synchronized whereas Hashtable is synchronized.
4. 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);

Hope this will be useful.
Please vote +1 or consider sharing if your like this article

19 comments:

ferdhie said...

what about ConcurrentHashMap? What is the difference?

Anonymous said...

That is a good explanation. Thanks.

Javin @ FIX Protocol Tutorial said...

Hi Fedhie, ConcurrentHashMap is new addition in Java collections suite and best suited for situations where number of readers outnumber number of writers in a big way. Since concurrent read is allowed it improves performance significantly.

Thanks
Javin

Javin @ FIX Protocol Tutorials said...

Hi Anonymous thanks you liked it you can also see How HashMap works in Java , to know more about HashMap interview questions.

Anand said...

Synchronizing a hashmap is possible but rather a pain because that is what the hashtable is there... But, synchronizing manually can be risky and at times lead to the most dreaded deadlocks. You can know more about "Synchronization & Deadlocks" here .

Anonymous said...

You can find similar article for collections with example in following link.

http://www.aoiblog.com/collections-in-java

Tutorials said...

thanks

Anonymous said...

Such a nice tutorial ....

Anonymous said...

can you care to explain difference bet fail-fast and fail-safe iterator on hashtable and hashmap , which one uses what, also hashmap is better or hashtable what is your opinion

Javin said...

Hi Anonymous fail-fast iterator means if one thread is iterating over hashmap and other thread trying to modify hashmap structurally it will throw ConcurrentModification Exception and fail immediately while in fail-safe iterator Iterations is done on copy of collection object instead of original e.g. in case of CopyOnWriteArraylist. iterator of hashmap is fail-fast and hashtable doesn't have any iterator that is another difference between hashmap and hashtable.

Anonymous said...

here are 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.

Anonymous said...

wow Anonymous got more differences between hashtable and hashmap :)

Neerja said...

I think key difference between hashmap and hastable is what you mentioned about Iterator and Enumeration , as Iterator being fail-fast beccomes natural choice over enumeration.

Anonymous said...

correct, though both hashtable and hashmap are older than new concurrenthashmap iterator and enumeration is key point.I would suggest to leave behind both hashmap and hashtable and move forward to concurrenthashMap.

nagesh said...

thanks for ur blog providing such great information for learners.

Javin @ spring interview questions answers said...

Thanks Nagesh for your comment and good to know that you like this hashtable vs hashmap tutorial. you may also like Difference between ArrayList and Vector in java

Anonymous said...

iterator can be implemented for hashtable also

Anonymous said...

from Java4 I guest hashtable is also part of collection framework along with Vector. with the age of ConcurrentHashMap, does Hashtable still holds value ?

Anonymous said...

sooooooo gooooodddddddd

Post a Comment

What is this blog about

This blog is about my experience in Java, Tibco Rendezvous and FIX protocol. FIX is a technology which is used to build equity trading system and heavily used in electronic trading , high frequency trading and Algorithmic trading, so on most of Investment bank job you need to know FIX Protocol on the other hand Tibco Rendezvous is used to implement messaging backbone on many big commercial and global banks. This blog help you to get clear any FIX protocol job interview. It also includes Java and Tibco Rendezvous article written for new user mainly my experience in my own word.