Take the 2-minute tour ×

I was looking up the difference between the two classes and this point came up in a lot of the answers with this blog being the source: http://javarevisited.blogspot.com/2010/10/difference-between-hashmap-and.html

However I don't completely get it. Can someone elaborate this? Perhaps with an example?

Thanks for looking in!

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

Fail-fast means when you try to modify the content when you are iterating thru it, it will fail and throw ConcurrentModificationException.

Set keys = hashMap.keySet();
for (Object key : key) {
    hashMap.put(someObject, someValue); //it will throw the ConcurrentModificationException here

For HashTable enumeration:

 Enumeration keys = hashTable.keys();
 for (Enumeration e = v.elements() ; e.hasMoreElements() ; e.nextElement()) {
      hashTable.put(someKey, someValue); //this is ok
share|improve this answer
The Hastable's iterators are fail-fast. Its enumerations are not. –  JB Nizet Jan 5 '12 at 23:00
You are right! Thanks! –  evanwong Jan 5 '12 at 23:05
add comment

When calling iterator.next(), if any modification has been made between the moment the iterator was created and the moment next() is called, a ConcurrentModificationException is immediately thrown. This is what fail-fast means.

The Enumerations returned by Hashtable don't have this behavior. They assume you know what you're doing, and their behavior, AFAIK, is undefined if you modify the map while iterating over it using one of its enumerations.

share|improve this answer
add comment

The best way is to probably look at the source for each class as implemented by the Open JDK implementation for each class; that way, you can get your answer straight from the horse's mouth, as it were :-)

That aside, essentially, "fail-fast" in this sense means that an Iterator over a HashMap will throw an exception if it detects that another thread has modified the targeted HashMap - if you look in the source for HashMap, you will see this is done by simply checking a counter for the number of expected modifications. If the modification count is different than the Iterator expected, that means that someone else has come in since the last check and messed around with the HashMap, and so the Iterator throws a ConcurrentModificationException.

A "non fail-fast" Iterator wouldn't bother to check, and happily go along it's business in the underlying data structure. Therefore, you gain some flexibility (probably dubious flexibility in this case) in exchange for possibly running into errors later; i.e. attempting to access a value that is no longer present.

As with all fail-fast strategies, the idea is that the earlier an error is detected, the easier it is to recover from or debug.

share|improve this answer
Not just another thread. If you modify the map while iterating over it, even in the same thread, a ConcurrentModification is thrown (except if you use the iterator itself to modify the map) –  JB Nizet Jan 5 '12 at 22:59
Very true! A sometimes confusing mistake I've done myself :-) –  pawosty Jan 5 '12 at 23:01
add comment

Your Answer


