Difference between fail-fast Iterator vs fail-safe Iterator in Java
Published April 25, 2022Whenever you want to retrieve elements one at a time, Java iterators are the best option. Java iterators are an integral part of the Java collection frameworks and they are very useful for handling exceptions. Iterators in Java usually fall into two categories: fail-safe and fail-fast.
In this article, we are going to discuss these two iterators with examples and then differentiate between them.
The Java Iterators
The Java iterator traverses a collection's objects, returning two types of iterators: fail fast and fail safe.
a) Fail fast Iterators
When a collection is structurally modified, the Fail Fast iterator immediately throws ConcurrentModificationException. During iteration over a dataset, structural modifications consist of adding, removing, and updating values. ArrayList, HashMap, and other collection classes are examples of Fail Fast iterators.
Fail Fast iterates a collection using a flag called modCount that keeps track of whether the collection has been structurally modified or not. Whenever a collection is modified, the modCount flag is updated; it checks the next value. If it does, then the modCount will be updated after this iterator has been created; otherwise, a ConcurrentModificationException will be raised.
Example of the Fail fast Iterator
package failfast; import java.util.Iterator; import java.util.concurrent.CopyOnWriteArrayList; public class failfastsample { public static void main(String[] args) { CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<Integer>(new Integer[] { 4,5,3,2,7 }); Iterator itr = list.iterator(); while (itr.hasNext()) { Integer i = (Integer)itr.next(); System.out.println(i); if (i == 7) list.add(15); } } } |
Output:
4
5
3
2
7
b) Fail Safe Iterator
Iterators that fail-safe are just the opposite of those that fail-fast, in that they do not throw exceptions unless the collection was modified during the iteration process. Rather than using the original object, they operate on a copy of the collection object. Therefore, the original collection will be left unchanged since structural changes on the original collection have been ignored.
Example Fail-Safe Iterator
package failSafe; import java.util.Iterator; import java.util.concurrent.ConcurrentHashMap; public class FailSafe { public static void main(String[] args) { ConcurrentHashMap<String, Integer> mymumbers = new ConcurrentHashMap<String, Integer>(); mymumbers.put("First Number", 10); mymumbers.put("Second Number", 34); mymumbers.put("Third Number", 23); mymumbers.put("Fourth Number", 18); Iterator it = mymumbers.keySet().iterator(); while (it.hasNext()) { String key = (String)it.next(); System.out.println(key + " : " + mymumbers.get(key)); mymumbers.put("Foutrth Number", 18);
} } } |
Output
Third Number : 23
First Number : 10
Second Number : 34
Fourth Number : 18
The difference between fail-fast iterator vs fail-safe iterator in java
The primary difference between the Fail-Safe and Fail-Fast is that when the object is modified during the iteration process, Fail Safe does not throw a ConcurrentModificationException, as opposed to Fail Fast, which throws such an exception. This is the result of the Fail-Safe iterator using the cloned collection instead of the original collection.
In terms of the memory utilization, the fast-fail iterator needs a low memory during the process while the fail-safe needs more memory during the process
Article Contributed By :
|
|
|
|
231 Views |