Saturday, June 4, 2022

What is fail safe and fail fast Iterator in Java?

Java Collections supports two types of Iterator, fail safe and fail fast. The main difference between a fail fast and fail safe Iterator is whether or not the underlying collection can be modified while its begin iteration. Fail safe Iterator allows that while fail fast Iterator doesn't, they throw ConcurrentModificatoinException when something goes wrong during iteration, except certain operations like removing an element using Iterator's remove() method. This behavior is implemented with a variable called modCount which keeps track of modification done on underlying collection. 

The key thing to remember here is which Collections support Fail fast iterator and which supports fail-safe. Most of the collection classes have fail-fast Iterator but certain concurrent collections like CopyOnWriteArrayLsit and CopyOnWriteHashSet support fail-safe Iterator.



Difference between Fail Safe and Fail Fast Iterator in Java

Now that you understand the key difference between fail-safe and fail-fast Iterator in Java. It's time  to go through more difference in a tabular and point format.

1. Acatual vs Copy Collection
The fail-fast Iterator directly works on the collection object itself while Fail safe iterator works on the clone or copy of the original collection object.


2. Modification during Iterator
Fail Fast Iterator doesn't allow you to modify the Collection while iterating while Fail safe iterator allows you to modify the collection during iteration. Former throws ConcurrentModificaitonException while later doesn't throw any error.


3. Extra Memory space
Since fail-fast iterator directly work on the collection they doesn't require extra memory which means they are suitable for large collection with thousands of elements while Fail-safe require extra memory as it works on the copy of collection, hence it consume more space in heap


4. Examples
Most of the collection classes from java.util package like ArrayList, HashMap, HashSet, Vector all support Fail fast iterator in Java but classes from java.util.concurrent package like ConcurrentHashMap, CopyOnWriteArrayList supports fail-safe iterator in Java.


5. When to use fail fast and fail safe Iterator
Use fail safe iterator when you are not bothered about Collection to be modified during iteration, as fail fast iterator will not allow that. Unfortunately you can't choose failsafe or fail fast iterator, it depends upon which Collection class you are using. 


Here is a nice table to remember these differences between fail-safe and fail-fast Iterator in Java:


What is fail safe and fail fast Iterator in Java?



That's all about the difference between fail-safe and fail-fast Iterator in Java. Most of the JDK 1.4 Collections like HashSet, Vector, ArrayList has fail fast Iterator and only Concurrent Collections introduced in JDK 1.5 like CopyOnWriteArrayList and CopyOnWriteHashSet supports fail safe Iteration.

Thanks for reading this article so far. If you like this article and difference between fail-safe and fail-fast Iterator in Java then please share with your friends and colleagues. If you have any questions or feedback then please drop a note. 

P. S. - If you are new to Java and want to master Java collection framework then you can also checkout this list of best Java Collection and Stream courses to learn Java Collection Framework in depth. It include best Java collections course from Udemy and Pluralsight.  


No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.