Solutions to Avoid ConcurrentModificationException When Removing Elements from ArrayList During Iteration

Nov 15, 2025 · Programming · 11 views · 7.8

Keywords: Java | ArrayList | ConcurrentModificationException | Iterator | Collection Operations

Abstract: This article provides an in-depth analysis of ConcurrentModificationException in Java and its solutions. By examining the causes of this exception when modifying ArrayList during iteration, it详细介绍介绍了使用Iterator的remove() method, traditional for loops, removeAll() method, and Java 8's removeIf() method. The article combines code examples and principle analysis to help developers understand concurrent modification control mechanisms in collections and provides best practice recommendations for real-world applications.

Problem Background and Exception Analysis

In Java programming, ArrayList as a commonly used collection class may throw ConcurrentModificationException when elements are modified directly during iteration. This exception is part of the fail-fast mechanism designed to prevent unexpected structural changes to collections during iteration.

Exception Generation Mechanism

The generation of ConcurrentModificationException stems from the internal checking mechanism of iterators. When using enhanced for loops to traverse ArrayList, the Java compiler converts it into an Iterator-based implementation. The iterator records the modification count (modCount) of the collection upon creation and checks whether this value has changed each time the next() method is called. If direct modification of the collection (not through the iterator itself) is detected, an exception is thrown.

Core Solution: Using Iterator.remove()

The most recommended solution is to use the remove() method of Iterator. This method can safely remove elements during iteration because the iterator internally synchronizes the modification count and collection state.

Iterator<String> iter = myArrayList.iterator();
while (iter.hasNext()) {
    String str = iter.next();
    if (someCondition) {
        iter.remove();
    }
}

The advantages of this method include:

Alternative Solution Analysis

Using Traditional For Loop

Accessing elements through indexes can avoid the concurrent checking of iterators, but index adjustment needs attention:

for (int i = 0; i < myArrayList.size(); i++) {
    if (someCondition) {
        myArrayList.remove(i);
        i--; // Adjust index to handle position changes after element removal
    }
}

Using removeAll Method

Record elements to be removed by creating a temporary collection, then remove them in batch:

List<String> toRemove = new ArrayList<>();
for (String str : myArrayList) {
    if (someCondition) {
        toRemove.add(str);
    }
}
myArrayList.removeAll(toRemove);

Although this method requires additional storage space, the code is clearer in certain scenarios.

Java 8's removeIf Method

In Java 8 and later versions, functional programming approach can be used:

myArrayList.removeIf(str -> someCondition);

This method is concise and efficient, making it one of the preferred solutions in modern Java development.

Performance and Applicability Comparison

Different solutions have their own characteristics in terms of performance and applicability:

Considerations in Multithreaded Environments

In concurrent environments, besides single-threaded modification issues, thread safety needs to be considered. It is recommended to use CopyOnWriteArrayList or appropriate synchronization mechanisms to ensure data consistency.

Best Practice Recommendations

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.