Fundamentals 6 min read

How to Remove Elements from a List Without Causing java.util.ConcurrentModificationException

This article explains why removing items from a Java List inside a foreach loop triggers a ConcurrentModificationException and demonstrates three correct approaches—using Iterator.remove(), a forward indexed for loop with index correction, and a reverse for loop—each with full code examples and output.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
How to Remove Elements from a List Without Causing java.util.ConcurrentModificationException

When beginners try to remove elements from a List using a foreach loop, a java.util.ConcurrentModificationException is thrown because the loop internally uses an Iterator that checks structural modifications via modCount and expectedModCount .

The article first shows the problematic code:

public static void main(String[] args) {
    List
platformList = new ArrayList<>();
    platformList.add("博客园");
    platformList.add("CSDN");
    platformList.add("掘金");

    for (String platform : platformList) {
        if (platform.equals("博客园")) {
            platformList.remove(platform);
        }
    }
    System.out.println(platformList);
}

Running this code results in a ConcurrentModificationException . The article then explains that the foreach construct uses an Iterator whose next() method calls checkForComodification() , comparing the two counters.

To avoid the exception, three safe removal methods are presented.

1. Using Iterator.remove()

public static void main(String[] args) {
    List
platformList = new ArrayList<>();
    platformList.add("博客园");
    platformList.add("CSDN");
    platformList.add("掘金");

    Iterator
iterator = platformList.iterator();
    while (iterator.hasNext()) {
        String platform = iterator.next();
        if (platform.equals("博客园")) {
            iterator.remove();
        }
    }
    System.out.println(platformList);
}

Output: [CSDN, 掘金]

2. Using a forward indexed for loop

public static void main(String[] args) {
    List
platformList = new ArrayList<>();
    platformList.add("博客园");
    platformList.add("CSDN");
    platformList.add("掘金");

    for (int i = 0; i < platformList.size(); i++) {
        String item = platformList.get(i);
        if (item.equals("博客园")) {
            platformList.remove(i);
            i = i - 1; // adjust index after removal
        }
    }
    System.out.println(platformList);
}

The index is decremented after a removal to avoid skipping the next element.

3. Using a reverse indexed for loop

public static void main(String[] args) {
    List
platformList = new ArrayList<>();
    platformList.add("博客园");
    platformList.add("CSDN");
    platformList.add("掘金");

    for (int i = platformList.size() - 1; i >= 0; i--) {
        String item = platformList.get(i);
        if (item.equals("掘金")) {
            platformList.remove(i);
        }
    }
    System.out.println(platformList);
}

Because the loop iterates from the end, no index correction is needed.

References: https://blog.csdn.net/zjwcdd/article/details/51513879 https://blog.csdn.net/wangjun5159/article/details/61415358

JavaArrayListIteratorfor loopConcurrentModificationExceptionremove
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.