Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do you exactly mean by HashMap's iterator is fail-fast and HashTable's enumerator isn't?

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!

like image 787
GrowinMan Avatar asked Jan 05 '12 22:01

GrowinMan


People also ask

What does it mean for an iterator to be fail-fast?

Fail-fast iterators checks the modCount flag whenever it gets the next value (i.e. using next() method), and if it finds that the modCount has been modified after this iterator has been created, it throws ConcurrentModificationException.

What is fail-fast and failsafe iterator?

Fail-Fast systems abort operation as-fast-as-possible exposing failures immediately and stopping the whole operation. Whereas, Fail-Safe systems don't abort an operation in the case of a failure. Such systems try to avoid raising failures as much as possible.

Why iterator in HashMap is fail-fast?

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.

What do you understand by fail-fast?

Fail fast is a philosophy that values extensive testing and incremental development to determine whether an idea has value. An important goal of the philosophy is to cut losses when testing reveals something isn't working and quickly try something else, a concept known as pivoting.


2 Answers

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.

like image 117
Paul Wostenberg Avatar answered Sep 24 '22 15:09

Paul Wostenberg


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 : keys) {
    hashMap.put(someObject, someValue); //it will throw the ConcurrentModificationException here
} 

For HashTable enumeration:

 Enumeration keys = hashTable.keys();
 while (keys.hasMoreElements()) {
          hashTable.put(someKey, someValue);  //this is ok
    }
like image 30
evanwong Avatar answered Sep 22 '22 15:09

evanwong