Fundamentals 5 min read

Why Removing Elements During forEach on an ArrayList Triggers ConcurrentModificationException and How to Fix It

The article explains that deleting elements from an ArrayList inside a forEach loop can cause a ConcurrentModificationException because the iterator's expected modification count diverges from the actual count, and it shows correct ways to remove items safely using an explicit Iterator or a CopyOnWriteArrayList.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Why Removing Elements During forEach on an ArrayList Triggers ConcurrentModificationException and How to Fix It

When an ArrayList is traversed with a forEach‑style loop and an element is removed directly via lists.remove("3") , the underlying iterator detects that the list's modCount no longer matches its expectedModCount , leading to a ConcurrentModificationException .

The article first demonstrates the problem with a simple list:

List<String> lists = new ArrayList<String>();

lists.add("1");
lists.add("2");
lists.add("3");
lists.add("4");

Removing the element "3" changes the internal array to ["1","2","4"] and reduces the size to 3. During a forEach traversal, the compiled code is equivalent to using an Iterator :

for (Iterator i = lists.iterator(); i.hasNext(); ) {
    String s = (String) i.next();
    if (s.equals("3")) {
        lists.remove(s);
    }
}

The iterator checks hasNext() by comparing the current cursor with the list size. After removing "3", the size becomes 3 while the cursor is already 3, so the loop ends and element "4" is never visited. If the list contains more elements (e.g., ["1","2","3","4","5"] ), the cursor is still less than the new size after removal, so the next call to i.next() finds a mismatch and throws the exception.

The root cause is that lists.remove("3") updates modCount but the iterator's expectedModCount remains unchanged, causing the inconsistency.

To delete elements safely while iterating, you must use the iterator’s own remove() method:

for (Iterator i = lists.iterator(); i.hasNext(); ) {
    String s = (String) i.next();
    if (s.equals("3")) {
        i.remove();
    }
}

Another approach is to replace ArrayList with CopyOnWriteArrayList , which creates a new copy of the underlying array on each modification, allowing the iterator to work on an immutable snapshot.

Both solutions prevent the ConcurrentModificationException and ensure correct traversal when elements are removed.

JavaArrayListIteratorforeachConcurrentModificationExceptioncopyonwritearraylist
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.