Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between fail-fast and fail safe in Java
In this article, we will find the differences between Fail-Fast and Fail-Safe Iterators. They both describe how collections behave when they are modified during iteration.
What is an iterator?
An Iterator is an object in Java used to cycle through a collection, accessing or removing elements. It can be obtained by using the iterator() method of a collection.What is FailSafe?
Fail-safe also called a Non-Fail-fast Iterator, does not throw ConcurrentModificationException. It works on a copy of the collection. Any changes made in the iterator only affect the copy but not the original collection.
Example
The following is an example of Fail-Safe in Java:
import java.util.concurrent.ConcurrentHashMap;
import java.util.Iterator;
public class FailSafeExample {
public static void main(String[] args) {
// Creating a ConcurrentHashMap
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
// Adding elements to map
map.put("Dell", 1);
map.put("IBM", 2);
// Getting an Iterator from map
Iterator<String> it = map.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
System.out.println(key + " : " + map.get(key));
map.put("Google", 3); // Adding new element while iterating
}
}
}
The output of the above Java program is:
Dell : 1 IBM : 2
What is Fail-Fast?
Fail-Fast is an iterator that throws a ConcurrentModificationException when a structural modification is made during iteration, such as adding or removing elements from the collection.
Example
The following is an example of Fail-Fast in Java:
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
public class FailFastExample {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
// Getting an Iterator from list
Iterator<Integer> it = list.iterator();
// Iterating through the list
while (it.hasNext()) {
Integer integer = it.next();
System.out.println(integer);
}
// Now modify the list after iteration
list.add(4);
System.out.println("Modified List: " + list);
}
}
The output of the above Java program is:
1 2 3 Modified List: [1, 2, 3, 4]
| Sr. No. | Key | Fail-Fast | Fail-Safe |
|---|---|---|---|
|
1 |
Exception |
Any changes in the collection, such as adding, removing and updating collection during a thread are iterating collection then Fail fast throw concurrent modification exception. |
The fail-safe collection doesn't throw exception. |
|
2. |
Type of collection |
ArrayList and hashmap collection are the examples of fail-fast iterator |
CopyOnWrite and concurrent modification are the examples of a fail-safe iterator |
|
3. |
Performance and Memory |
It's work on actual collection instead. So, this iterator doesn't require extra memory and time |
It's working on a clone of the collection instead of actual collection. It is overhead in terms of time and memory |
|
4. |
Modifications |
Iterators don't allow modifications of a collection while iterating over it. |
Fail-Safe iterators allow modifications of a collection while iterating over it. |