Java / Editorial solution guide
Diagnose ConcurrentModificationException during iteration
Find structural changes and select a valid iteration strategy.
Read-only knowledge base · No public comments
Recognize the symptoms
A collection traversal throws ConcurrentModificationException after a modification.
Understand the likely causes
- The collection was structurally changed outside the iterator’s permitted operation.
Reproduce the iteration
Preserve the stack trace, collection type, and sequence of operations that led to the exception. Reproduce with a small synthetic list and identify both the iterator and every structural modification during traversal. The exception can occur in a single thread; its name alone does not prove a data race. Avoid using production records in a reproduction when fixed sample values show the same behavior.
Identify the modification
Many general-purpose collection iterators detect some structural modifications outside their own supported operations. Their fail-fast behavior is best effort, not a synchronization mechanism or a guarantee that every invalid modification will throw. Determine whether the list is changed directly in the loop, by a callback, or by another thread. Each source needs an ownership and coordination decision rather than merely catching the exception.
Choose a valid removal strategy
For a single-threaded traversal whose iterator supports removal, remove the current element through that iterator. The example uses an ArrayList iterator and removes both matching values without modifying the list directly during traversal. Another valid design may build a new collection or collect changes for a later phase. For genuinely concurrent access, select an appropriate synchronization or collection strategy based on the required iteration semantics.
When collecting removals for a later phase, ensure the later operation uses the intended equality or identity rule. Recheck the collection after applying all changes, especially when duplicate values are valid.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> items = new ArrayList<>(List.of("red", "blue", "red"));
Iterator<String> it = items.iterator();
while (it.hasNext()) {
if (it.next().equals("red")) it.remove();
}
System.out.println(items);
}
}
[blue]
Iterator.remove removes the current element during this traversal.
Verify the resulting collection
Repeat the original operation and verify the resulting elements, including adjacent matches, no matches, and an empty list. If another thread participates, use controlled coordination and verify the chosen synchronization contract rather than relying on the exception to reveal a race. Test any callback that can modify the collection. A run without an exception is not by itself proof that concurrent modification is safe.
Verify the fix
The original traversal produces the expected contents under its documented ownership and synchronization rules.
Keep it from recurring
- Document mutation ownership and use an iteration strategy appropriate to the collection.