ConcurrentModificationException in Java — Fix (2026) | Tutorials Logic
What is This Error?
The ConcurrentModificationException is thrown when a collection is modified while it is being iterated using an iterator or enhanced for-each loop. Java's fail-fast iterators detect structural modifications and throw this exception to prevent unpredictable behavior.
Error Message:
Exception in thread "main" java.util.ConcurrentModificationExceptionat java.util.ArrayList$Itr.checkForComodification(ArrayList.java:911)
Common Causes
Quick Fix (TL;DR)
// ❌ Problem
for (String item : list) {
if (item.equals("remove")) list.remove(item); // ConcurrentModificationException!
}
// ✅ Solution 1: Use Iterator
Iterator it = list.iterator();
while (it.hasNext()) {
if (it.next().equals("remove")) it.remove(); // Safe!
}
// ✅ Solution 2: removeIf (Java 8+)
list.removeIf(item -> item.equals("remove"));
Common Scenarios & Solutions
Scenario 1: Removing During For-Each
List numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
for (Integer num : numbers) {
if (num % 2 == 0) {
numbers.remove(num); // ❌ ConcurrentModificationException!
}
}
List numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
// ✅ Option 1: removeIf (cleanest)
numbers.removeIf(num -> num % 2 == 0);
// ✅ Option 2: Iterator
Iterator it = numbers.iterator();
while (it.hasNext()) {
if (it.next() % 2 == 0) it.remove();
}
// ✅ Option 3: Collect to remove list
List toRemove = numbers.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
numbers.removeAll(toRemove);
// ✅ Option 4: Stream filter (creates new list)
numbers = numbers.stream()
.filter(n -> n % 2 != 0)
.collect(Collectors.toList());
Scenario 2: Multi-threaded Modification
// ✅ Use CopyOnWriteArrayList for thread-safe iteration
List list = new CopyOnWriteArrayList<>(Arrays.asList("a", "b", "c"));
for (String s : list) {
list.add("d"); // Safe — iterates over snapshot
}
// ✅ Or use synchronized block
List syncList = Collections.synchronizedList(new ArrayList<>());
synchronized (syncList) {
for (String s : syncList) {
// Safe iteration
}
}
Scenario 3: Modifying Map During Iteration
Map map = new HashMap<>();
map.put("a", 1); map.put("b", 2); map.put("c", 3);
for (Map.Entry entry : map.entrySet()) {
if (entry.getValue() < 2) map.remove(entry.getKey()); // ❌ CME!
}
// ✅ Use Iterator on entrySet
Iterator> it = map.entrySet().iterator();
while (it.hasNext()) {
if (it.next().getValue() < 2) it.remove();
}
// ✅ Or use entrySet().removeIf (Java 8+)
map.entrySet().removeIf(e -> e.getValue() < 2);
Best Practices to Avoid This Error
Related Errors
Key Takeaways
- ConcurrentModificationException is thrown when a collection is modified during iteration
- Use removeIf() for the cleanest conditional removal in Java 8+
- Use Iterator.remove() for safe removal during manual iteration
- Collect elements to remove first, then call removeAll() after iteration
- Use CopyOnWriteArrayList or ConcurrentHashMap for thread-safe concurrent access
- Stream.filter() creates a new collection without modifying the original
Frequently Asked Questions
Level Up Your Core java Skills
Master Core java with these hand-picked resources
10,000+ learners
Free forever
Updated 2026
Related Java Topics