In this interview topic, we will discuss the differences between fail-fast and fail-safe iterators in Java. This question is closely related to the ConcurrentModificationException and the scenarios in which this exception is thrown.
Related topic - Difference between HashSet and TreeSet in Java
Difference between fail-fast and fail-safe iterator:-
Example of fail-fast iterator:-
As shown in the diagram, the primary difference between fail-fast and fail-safe iterators is how they handle ConcurrentModificationException.
Fail-fast iterators internally maintain a modification count (
FailFastIteratorExample.java,
Output:-
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at com.example.demo.FailFastIteratorExample.main(FailFastIteratorExample.java:17)
Example Of fail-safe iterator:-
The fail-safe iterator doesn't throw any exception if the collection is modified while iterating over it. Internally it will copy the original collection object, and iterate over the copied collection object. Due to this internal behavior, if any modification done on collection, it doesn't impact on iterator.
FailSafeIteratorExample.java,
Output:-
A
B
C
C
Related Posts:-
1) Collection Hierarchy in Java
2) Collection Interview Questions and Answers in Java(List,Map & Set)
3) How to iterate the TreeMap in reverse order in Java
4) How to Remove duplicates from ArrayList in Java
5) Internal Implementation of TreeMap in Java
6) Internal implementation of ArrayList in Java
7) Internal Implementation of LinkedList in Java
8) Difference between HashSet and TreeSet in Java
A fail-fast iterator throws a ConcurrentModificationException when the underlying collection is modified during iteration, whereas a fail-safe iterator does not throw this exception because it operates on a copy of the collection. This is the primary difference between the two.
Let's explore a few more differences between fail-fast and fail-safe iterators along with examples.
Related topic - Difference between HashSet and TreeSet in Java
Difference between fail-fast and fail-safe iterator:-
![]() |
| Difference between fail-fast and fail-safe iterator |
Example of fail-fast iterator:-
As shown in the diagram, the primary difference between fail-fast and fail-safe iterators is how they handle ConcurrentModificationException.
In most collection implementations, elements are stored internally using data structures such as arrays or hash tables. When iterating over a collection, modifying the underlying collection directly is generally not allowed.
Fail-fast iterators internally maintain a modification count (
modCount) to detect structural changes to the collection. During iteration, the iterator compares the current modification count with the expected modification count. If they differ, it indicates that the collection has been modified, and a ConcurrentModificationException is thrown..FailFastIteratorExample.java,
package com.example.demo;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class FailFastIteratorExample {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
iterator.next();
list.add("C");
}
}
}
Output:-
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at com.example.demo.FailFastIteratorExample.main(FailFastIteratorExample.java:17)
Example Of fail-safe iterator:-
The fail-safe iterator doesn't throw any exception if the collection is modified while iterating over it. Internally it will copy the original collection object, and iterate over the copied collection object. Due to this internal behavior, if any modification done on collection, it doesn't impact on iterator.
FailSafeIteratorExample.java,
package com.example.demo;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public class FailSafeIteratorExample {
public static void main(String[] args) {
List<String> list = new CopyOnWriteArrayList<String>();
list.add("A");
list.add("B");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
iterator.next();
list.add("C");
}
list.forEach(str -> System.out.println(str));
}
}
Output:-
A
B
C
C
Related Posts:-
1) Collection Hierarchy in Java
2) Collection Interview Questions and Answers in Java(List,Map & Set)
3) How to iterate the TreeMap in reverse order in Java
4) How to Remove duplicates from ArrayList in Java
5) Internal Implementation of TreeMap in Java
6) Internal implementation of ArrayList in Java
7) Internal Implementation of LinkedList in Java
8) Difference between HashSet and TreeSet in Java

