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.
19 comments:
what about ConcurrentHashMap? What is the difference?
That is a good explanation. Thanks.
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
Hi Anonymous thanks you liked it you can also see How HashMap works in Java , to know more about HashMap interview questions.
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 .
You can find similar article for collections with example in following link.
http://www.aoiblog.com/collections-in-java
thanks
Such a nice tutorial ....
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
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.
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.
wow Anonymous got more differences between hashtable and hashmap :)
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.
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.
thanks for ur blog providing such great information for learners.
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
iterator can be implemented for hashtable also
from Java4 I guest hashtable is also part of collection framework along with Vector. with the age of ConcurrentHashMap, does Hashtable still holds value ?
sooooooo gooooodddddddd
Post a Comment