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 Iterator: hasNext() and next() are called behind the scenes. The iterators of ArrayList, LinkedList, HashMap and the other standard collections are fail-fast. The collection keeps a counter, modCount, that grows with every structural change, meaning any change to the number of elements. The iterator records the counter when it is created and checks it on every next(). If the list was changed by anything other than the iterator itself, next() throws ConcurrentModificationException.

Despite the name, no second thread is needed: a single thread that calls list.remove inside its own for-each loop triggers it. The check is a best-effort bug detector, not a guarantee; in some positions the loop just ends early without any exception.

Safe ways to remove during iteration
ApproachSinceBest for
list.removeIf(condition)Java 8The condition is all you need; fastest
Iterator.remove()Java 1.2The loop also does other work per element
ListIterator remove, set, addJava 1.2Removing, replacing and inserting in one pass
Index loop from the endAnyCode that needs the index
Collect, then removeAllJava 1.2Decisions that need to see the whole list first
Filter into a new listJava 8 (streams)When a new list is acceptable
CopyOnWriteArrayListJava 5Lists shared between threads

All programs ran on JDK 21. For removing outside loops, see removing elements from a list.

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.

Java
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:

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 next() detected the change and threw. The list was left half-processed: done:a is gone but done:c is still there.
  • In the second list, removing done:y made the size 2 while the iterator's cursor was also 2, so hasNext() returned false and the loop ended without an exception. done:z was never examined and stayed in the list. This silent case is worse than the exception.

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.

Java
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:

Output
after set:   b
after add:   ConcurrentModificationException
removed y while looping, z never visited: [x, z]
second iterator: ConcurrentModificationException
  • set does not change the size, so it is not a structural change and the iterator continued; add is structural, and the next next() threw.
  • Removing the second-to-last element ended the loop early without an exception, the silent case again.
  • Iterator.remove() keeps only its own iterator in sync; any other open iterator over the list is invalidated.

How fail-fast checking works in detail, and how snapshot and weakly consistent iterators in concurrent collections differ, is explained on the Iterator and ListIterator page. The rest of this page is about removing safely and what each technique costs.

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.

Java
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:

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 ArrayList each remove(i) still shifts the rest of the array, so for many removals this is slow; removeIf does the same job in one pass.

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.

Java
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:

Output
removeIf:        [open:b, open:d]
iterator.remove: [open:b, open:d] (2 removed)
stream filter:   [open:b, open:d]
remove() twice:  IllegalStateException
  • removeIf is one line and, on an ArrayList, runs in a single pass however many elements match. Use it whenever the condition is all you need.
  • Iterator.remove() removes the element most recently returned by next(), through the iterator, so the iterator stays valid. The loop can do other work too, such as counting what it removed.
  • A stream with filter builds a new list instead of changing the old one, which avoids the problem entirely.
  • Calling remove() twice without a next() in between, or before the first next(), throws IllegalStateException: there is no current element to remove.

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.

Java
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:

Output
ListIterator edits: [OPEN:A, OPEN:C, d, e]
replaceAll:         [open:a, open:c, d, e]
CopyOnWriteArrayList: [y]
keep only the maximum: [90, 90]
  • ListIterator adds set, which replaces the current element, and add, which inserts after it. The loop removed done:b, uppercased the two open items, and split d+e into two elements. The inserted e is placed behind the cursor, so the loop does not visit it.
  • replaceAll changes every element in one call when no removal is needed.
  • A CopyOnWriteArrayList 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; see CopyOnWriteArrayList.
  • When the rule depends on the whole list, such as "keep only the maximum", collect the elements to remove first and call removeAll afterwards.

The differences between Iterator and ListIterator are covered on the iterator page. Removing entries from a map during a loop has the same rules; see removing from a map.

Lambdas and streams are loops too

Replacing the for-each loop with forEach or a stream does not change the rule: they still iterate over the list, and changing the list from inside them breaks the iteration. removeIf is different, and the program shows how, including two less obvious consequences of the way it works.

Java
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:

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
  • forEach with tasks.remove threw ConcurrentModificationException after removing done:a, leaving done:c behind. ArrayList.forEach checks the modification count when it finishes.
  • The stream version removed both done tasks and then failed with NullPointerException: 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.
  • ArrayList.removeIf 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 failed removeIf safe to retry after fixing the data. A hand-written iterator loop would have left the list half-processed.
  • For the same reason, a predicate that reads the list sees it as it was before any removal: nums.size() was 6 for every test, so 1 to 5 were removed. Avoid predicates that depend on the list being filtered.
  • List.of(...).removeIf threw UnsupportedOperationException even when no element matched, because an immutable list rejects the call itself. Copy such lists into an ArrayList first.

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 ArrayList and LinkedList.

Java
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):

Output
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, removeIf 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.
  • iterator.remove() 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.
  • Collecting the odd numbers and calling removeAll with a List was the slowest of all, because removeAll calls contains on its argument for every element: a linear search each time. With a HashSet argument the same idea took a few milliseconds.
  • On LinkedList, iterator.remove() was fast, because unlinking the current node costs the same anywhere. But the index loop was the slowest result on the page: get(i) and remove(i) walk the list from one end every time. On a linked list, always remove through an iterator.

Which approach to use

Choosing how to remove during iteration
SituationUse
Remove by a conditionlist.removeIf(condition)
Remove and do other work in the same loopAn explicit Iterator and it.remove()
Remove, replace and insert in one passListIterator
The loop needs the indexAn index loop from the last element down
Decision needs the whole listCollect the elements, then removeAll
A new list is finestream().filter(...).toList()
List shared with other threadsCopyOnWriteArrayList or a lock around the loop
Many removals from an ArrayList inside a loopAvoid: collect into a HashSet and call removeAll, or use removeIf
Removals from a LinkedListAn iterator, never index-based remove(i)
Neverlist.remove inside a for-each loop, forEach or stream over the same list