Remove Elements While Iterating
Removing elements from a list inside a loop is one of the most common sources of ConcurrentModificationException and of quietly skipped elements. This page explains the fail-fast mechanism behind the exception, reproduces the three classic mistakes, and shows the safe ways: removeIf, Iterator.remove, ListIterator edits, backward index loops and collecting first.
Contents
Why removing in a loop goes wrong
A for-each loop over a list is compiled into an IteratorhasNext()next()ArrayListLinkedListHashMapmodCountnext()next()ConcurrentModificationException
Despite the name, no second thread is needed: a single thread that calls list.remove
| Approach | Since | Best for |
|---|---|---|
list.removeIf(condition) | Java 8 | The condition is all you need; fastest |
Iterator.remove() | Java 1.2 | The loop also does other work per element |
ListIteratorremovesetadd | Java 1.2 | Removing, replacing and inserting in one pass |
| Index loop from the end | Any | Code that needs the index |
Collect, then removeAll | Java 1.2 | Decisions that need to see the whole list first |
| Filter into a new list | Java 8 (streams) | When a new list is acceptable |
CopyOnWriteArrayList | Java 5 | Lists shared between threads |
All programs ran on JDK 21. For removing outside loops, see
The for-each mistake
The task list alternates done and open tasks, so the loop has several elements to remove. A second list is chosen so that the removal hits the second-to-last element.
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.List;
public class ForEachFails {
public static void main(String[] args) {
List<String> tasks = new ArrayList<>(List.of("done:a", "open:b", "done:c", "open:d"));
try {
for (String t : tasks) {
if (t.startsWith("done")) {
tasks.remove(t);
}
}
} catch (ConcurrentModificationException e) {
System.out.println("for-each + list.remove: ConcurrentModificationException");
}
System.out.println("list is now: " + tasks);
// The odd case: removing the SECOND-TO-LAST element does not throw,
// the loop just stops early and never checks the last element
List<String> sneaky = new ArrayList<>(List.of("x", "done:y", "done:z"));
for (String t : sneaky) {
if (t.startsWith("done")) {
sneaky.remove(t);
}
}
System.out.println("no exception, but: " + sneaky);
}
}
Output:
for-each + list.remove: ConcurrentModificationException
list is now: [open:b, done:c, open:d]
no exception, but: [x, done:z]
- The first removal succeeded, and the following call to
detected the change and threw. The list was left half-processed:next() is gone butdone:a is still there.done:c - In the second list, removing
made the size 2 while the iterator's cursor was also 2, sodone:y returned false and the loop ended without an exception.hasNext() was never examined and stayed in the list. This silent case is worse than the exception.done:z
What the iterator actually checks
To see which changes break a loop, the program changes a list in different ways while an iterator is open, and uses two iterators on the same list.
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;
public class HowCmeWorks {
public static void main(String[] args) {
// The iterator remembers modCount; any structural change through the list bumps it
List<String> list = new ArrayList<>(List.of("a", "b", "c", "d"));
Iterator<String> it = list.iterator();
it.next();
list.set(0, "A"); // set is not structural: no problem
System.out.println("after set: " + it.next());
list.add("e"); // structural: the iterator is now invalid
try {
it.next();
} catch (ConcurrentModificationException e) {
System.out.println("after add: ConcurrentModificationException");
}
// Removing the second-to-last element: the loop ends early, no exception
List<String> tail = new ArrayList<>(List.of("x", "y", "z"));
for (String s : tail) {
if (s.equals("y")) {
tail.remove(s);
}
}
System.out.println("removed y while looping, z never visited: " + tail);
// The same mistake with two iterators: the second one fails
List<Integer> nums = new ArrayList<>(List.of(1, 2, 3));
Iterator<Integer> first = nums.iterator();
Iterator<Integer> second = nums.iterator();
first.next();
first.remove(); // fine for first, invalidates second
try {
second.next();
} catch (ConcurrentModificationException e) {
System.out.println("second iterator: ConcurrentModificationException");
}
}
}
Output:
after set: b
after add: ConcurrentModificationException
removed y while looping, z never visited: [x, z]
second iterator: ConcurrentModificationException
does not change the size, so it is not a structural change and the iterator continued;set is structural, and the nextadd threw.next()- Removing the second-to-last element ended the loop early without an exception, the silent case again.
keeps only its own iterator in sync; any other open iterator over the list is invalidated.Iterator.remove()
How fail-fast checking works in detail, and how snapshot and weakly consistent iterators in concurrent collections differ, is explained on the
The index loop that skips elements
Switching to an index loop avoids the exception but introduces a different bug. The list has two negative numbers next to each other, which is exactly where it shows.
import java.util.ArrayList;
import java.util.List;
public class IndexLoopSkips {
public static void main(String[] args) {
// Two adjacent negatives: the second one gets skipped
List<Integer> forward = new ArrayList<>(List.of(5, -1, -2, 7, -3));
for (int i = 0; i < forward.size(); i++) {
if (forward.get(i) < 0) {
forward.remove(i); // everything after i shifts left by one
}
}
System.out.println("forward loop: " + forward);
// Going backwards, a shift only affects indexes already visited
List<Integer> backward = new ArrayList<>(List.of(5, -1, -2, 7, -3));
for (int i = backward.size() - 1; i >= 0; i--) {
if (backward.get(i) < 0) {
backward.remove(i);
}
}
System.out.println("backward loop: " + backward);
}
}
Output:
forward loop: [5, -2, 7]
backward loop: [5, 7]
- The forward loop removed -1 at index 1, so -2 moved to index 1. The loop then went on to index 2 and never looked at -2, which stayed in the result.
- Looping from the end avoids that: removing at index i only moves elements after i, which have already been visited. The backward loop removed all three negatives.
- On an
eachArrayList still shifts the rest of the array, so for many removals this is slow;remove(i) does the same job in one pass.removeIf
The safe ways
Each approach starts from the same four tasks and removes the two that are done, so the results can be compared directly.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class SafeWays {
public static void main(String[] args) {
List<String> source = List.of("done:a", "open:b", "done:c", "open:d");
// 1. removeIf: the simplest when the condition is all you need
List<String> a = new ArrayList<>(source);
a.removeIf(t -> t.startsWith("done"));
System.out.println("removeIf: " + a);
// 2. Iterator.remove: when the loop also does other work per element
List<String> b = new ArrayList<>(source);
Iterator<String> it = b.iterator();
int removed = 0;
while (it.hasNext()) {
String t = it.next();
if (t.startsWith("done")) {
it.remove(); // removes the element last returned by next()
removed++;
}
}
System.out.println("iterator.remove: " + b + " (" + removed + " removed)");
// 3. Build a new list instead of changing the old one
List<String> c = source.stream().filter(t -> !t.startsWith("done")).toList();
System.out.println("stream filter: " + c);
// Iterator.remove twice in a row is an error
Iterator<String> twice = new ArrayList<>(source).iterator();
twice.next();
twice.remove();
try {
twice.remove();
} catch (IllegalStateException e) {
System.out.println("remove() twice: IllegalStateException");
}
}
}
Output:
removeIf: [open:b, open:d]
iterator.remove: [open:b, open:d] (2 removed)
stream filter: [open:b, open:d]
remove() twice: IllegalStateException
is one line and, on anremoveIf , runs in a single pass however many elements match. Use it whenever the condition is all you need.ArrayList removes the element most recently returned byIterator.remove() , through the iterator, so the iterator stays valid. The loop can do other work too, such as counting what it removed.next()- A stream with
builds a new list instead of changing the old one, which avoids the problem entirely.filter - Calling
twice without aremove() in between, or before the firstnext() , throwsnext() : there is no current element to remove.IllegalStateException
Editing with ListIterator, and other options
Sometimes a pass over the list must remove some elements, change others and insert new ones. The task list includes one of each kind of edit, followed by two alternatives for special cases.
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.CopyOnWriteArrayList;
public class ListIteratorEdits {
public static void main(String[] args) {
// ListIterator can remove, replace and insert while walking
List<String> tasks = new ArrayList<>(List.of("open:a", "done:b", "open:c", "split:d+e"));
ListIterator<String> it = tasks.listIterator();
while (it.hasNext()) {
String t = it.next();
if (t.startsWith("done:")) {
it.remove();
} else if (t.startsWith("open:")) {
it.set(t.toUpperCase());
} else if (t.startsWith("split:")) {
String[] parts = t.substring(6).split("\\+");
it.set(parts[0]);
it.add(parts[1]); // inserted after, not visited by this loop
}
}
System.out.println("ListIterator edits: " + tasks);
// replaceAll changes every element in one call
tasks.replaceAll(s -> s.toLowerCase());
System.out.println("replaceAll: " + tasks);
// A CopyOnWriteArrayList can be modified during a for-each loop
List<String> cow = new CopyOnWriteArrayList<>(List.of("x", "y", "z"));
for (String s : cow) {
if (!s.equals("y")) {
cow.remove(s);
}
}
System.out.println("CopyOnWriteArrayList: " + cow);
// Collect first, remove after: when the decision needs the whole list
List<Integer> scores = new ArrayList<>(List.of(50, 90, 70, 90, 60));
int max = scores.stream().mapToInt(Integer::intValue).max().orElse(0);
List<Integer> toRemove = new ArrayList<>();
for (int s : scores) {
if (s < max) {
toRemove.add(s);
}
}
scores.removeAll(toRemove);
System.out.println("keep only the maximum: " + scores);
}
}
Output:
ListIterator edits: [OPEN:A, OPEN:C, d, e]
replaceAll: [open:a, open:c, d, e]
CopyOnWriteArrayList: [y]
keep only the maximum: [90, 90]
addsListIterator , which replaces the current element, andset , which inserts after it. The loop removedadd , uppercased the two open items, and splitdone:b into two elements. The insertedd+e is placed behind the cursor, so the loop does not visit it.e changes every element in one call when no removal is needed.replaceAll- A
iterates over a snapshot, so removing during a for-each loop is allowed. Each removal copies the array, so this suits small shared lists only; seeCopyOnWriteArrayListCopyOnWriteArrayList . - When the rule depends on the whole list, such as "keep only the maximum", collect the elements to remove first and call
afterwards.removeAll
The differences between IteratorListIterator
Lambdas and streams are loops too
Replacing the for-each loop with forEachremoveIf
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.List;
public class LambdaTraps {
public static void main(String[] args) {
// forEach with a lambda is a loop too
List<String> tasks = new ArrayList<>(List.of("done:a", "open:b", "done:c"));
try {
tasks.forEach(t -> {
if (t.startsWith("done")) {
tasks.remove(t);
}
});
} catch (ConcurrentModificationException e) {
System.out.println("forEach + remove: ConcurrentModificationException, list "
+ tasks);
}
// So is a stream over the list
List<String> tasks2 = new ArrayList<>(List.of("done:a", "open:b", "done:c"));
try {
tasks2.stream().filter(t -> t.startsWith("done")).forEach(tasks2::remove);
} catch (RuntimeException e) {
System.out.println("stream + remove: " + e.getClass().getSimpleName()
+ ", list " + tasks2);
}
// removeIf decides first and removes afterwards, so a failing predicate changes nothing
List<String> raw = new ArrayList<>(List.of("10", "-3", "x", "-7", "4"));
try {
raw.removeIf(s -> Integer.parseInt(s) < 0);
} catch (NumberFormatException e) {
System.out.println("removeIf, predicate threw at \"x\": list unchanged " + raw);
}
// A predicate that reads the list sees it before any removal
List<Integer> nums = new ArrayList<>(List.of(1, 2, 3, 4, 5, 6));
try {
nums.removeIf(n -> n < nums.size()); // reads the list itself
System.out.println("removeIf(n -> n < nums.size()): " + nums);
} catch (ConcurrentModificationException e) {
System.out.println("removeIf reading the list: ConcurrentModificationException");
}
// removeIf on a list that cannot change
try {
List.of(1, -2).removeIf(n -> n < 0);
} catch (UnsupportedOperationException e) {
System.out.println("List.of(...).removeIf: UnsupportedOperationException");
}
System.out.println("List.of(...).removeIf with no match: "
+ tryRemove(List.of(1, 2)));
}
static String tryRemove(List<Integer> list) {
try {
return "returned " + list.removeIf(n -> n < 0);
} catch (UnsupportedOperationException e) {
return "UnsupportedOperationException";
}
}
}
Output:
forEach + remove: ConcurrentModificationException, list [open:b, done:c]
stream + remove: NullPointerException, list [open:b]
removeIf, predicate threw at "x": list unchanged [10, -3, x, -7, 4]
removeIf(n -> n < nums.size()): [6]
List.of(...).removeIf: UnsupportedOperationException
List.of(...).removeIf with no match: UnsupportedOperationException
withforEach threwtasks.remove after removingConcurrentModificationException , leavingdone:a behind.done:c checks the modification count when it finishes.ArrayList.forEach- The stream version removed both done tasks and then failed with
: the stream kept reading array slots that the removals had shifted and emptied. Depending on the data, the same code can throw, skip elements or appear to work, so the result is never reliable.NullPointerException first tests every element and only then removes the matches. When the predicate threw on "x", nothing had been removed yet and the list was unchanged, which makes a failedArrayList.removeIf safe to retry after fixing the data. A hand-written iterator loop would have left the list half-processed.removeIf- For the same reason, a predicate that reads the list sees it as it was before any removal:
was 6 for every test, so 1 to 5 were removed. Avoid predicates that depend on the list being filtered.nums.size() threwList.of(...).removeIf even when no element matched, because an immutable list rejects the call itself. Copy such lists into anUnsupportedOperationException first.ArrayList
What each technique costs
The safe techniques differ a lot in speed. The program removes the 50,000 odd numbers from a list of 100,000 with each of them, best of 3 runs, on both ArrayListLinkedList
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class RemovalCost {
static final int N = 100_000;
static List<Integer> filled(Supplier<List<Integer>> factory) {
List<Integer> l = factory.get();
for (int i = 0; i < N; i++) {
l.add(i);
}
return l;
}
static void measure(String label, Supplier<List<Integer>> factory,
Consumer<List<Integer>> removeOdd) {
long best = Long.MAX_VALUE;
int left = 0;
for (int r = 0; r < 3; r++) {
List<Integer> l = filled(factory);
long start = System.nanoTime();
removeOdd.accept(l);
best = Math.min(best, System.nanoTime() - start);
left = l.size();
}
System.out.printf("%-36s %8.1f ms (%,d left)%n", label, best / 1e6, left);
}
static void iteratorRemove(List<Integer> l) {
for (Iterator<Integer> it = l.iterator(); it.hasNext(); ) {
if (it.next() % 2 != 0) {
it.remove();
}
}
}
public static void main(String[] args) {
System.out.println("remove the 50,000 odd numbers from 100,000, best of 3:");
measure("ArrayList removeIf", ArrayList::new, l -> l.removeIf(n -> n % 2 != 0));
measure("ArrayList iterator.remove()", ArrayList::new, RemovalCost::iteratorRemove);
measure("ArrayList index loop from the end", ArrayList::new, l -> {
for (int i = l.size() - 1; i >= 0; i--) {
if (l.get(i) % 2 != 0) {
l.remove(i);
}
}
});
measure("ArrayList collect, removeAll(List)", ArrayList::new, l -> {
List<Integer> odd = new ArrayList<>();
for (int n : l) {
if (n % 2 != 0) {
odd.add(n);
}
}
l.removeAll(odd);
});
measure("ArrayList collect, removeAll(HashSet)", ArrayList::new, l -> {
HashSet<Integer> odd = new HashSet<>();
for (int n : l) {
if (n % 2 != 0) {
odd.add(n);
}
}
l.removeAll(odd);
});
measure("LinkedList iterator.remove()", LinkedList::new, RemovalCost::iteratorRemove);
measure("LinkedList index loop from the end", LinkedList::new, l -> {
for (int i = l.size() - 1; i >= 0; i--) {
if (l.get(i) % 2 != 0) {
l.remove(i);
}
}
});
}
}
Output (rough comparison on a 2-CPU Linux VM, JDK 21; absolute times will differ on yours):
remove the 50,000 odd numbers from 100,000, best of 3:
ArrayList removeIf 2.9 ms (50,000 left)
ArrayList iterator.remove() 181.5 ms (50,000 left)
ArrayList index loop from the end 86.9 ms (50,000 left)
ArrayList collect, removeAll(List) 2050.0 ms (50,000 left)
ArrayList collect, removeAll(HashSet) 7.5 ms (50,000 left)
LinkedList iterator.remove() 1.6 ms (50,000 left)
LinkedList index loop from the end 4715.7 ms (50,000 left)
- On
,ArrayList took a few milliseconds. It marks the matches in one pass and compacts the array in a second, so each remaining element moves at most once.removeIf and the backward index loop were tens of times slower. Each removal shifts every element after it, so the total work grows with the square of the list size. The backward loop was faster than the iterator only because its shifts are shorter on average.iterator.remove()- Collecting the odd numbers and calling
with aremoveAll was the slowest of all, becauseList callsremoveAll on its argument for every element: a linear search each time. With acontains argument the same idea took a few milliseconds.HashSet - On
,LinkedList was fast, because unlinking the current node costs the same anywhere. But the index loop was the slowest result on the page:iterator.remove() andget(i) walk the list from one end every time. On a linked list, always remove through an iterator.remove(i)
Which approach to use
| Situation | Use |
|---|---|
| Remove by a condition | list.removeIf(condition) |
| Remove and do other work in the same loop | An explicit Iteratorit.remove() |
| Remove, replace and insert in one pass | ListIterator |
| The loop needs the index | An index loop from the last element down |
| Decision needs the whole list | Collect the elements, then removeAll |
| A new list is fine | stream().filter(...).toList() |
| List shared with other threads | CopyOnWriteArrayList |
Many removals from an ArrayList | Avoid: collect into a HashSetremoveAllremoveIf |
Removals from a LinkedList | An iterator, never index-based remove(i) |
| Never | list.removeforEach |