TryBugHunt

java.lang.ConcurrentModificationException

java.lang.ConcurrentModificationException in Java means a Java collection was modified structurally (elements added or removed) while a thread or iterator was actively traversing it.

Java

How do I fix java.lang.ConcurrentModificationException?

To fix java.lang.ConcurrentModificationException in Java, locate the line causing the error and check the values right before it runs. Most occurrences happen because of calling list.remove() inside an enhanced for-loop. Apply a defensive check or safe fallback, make sure collections or objects exist before access, and verify return values from upstream functions.

1. Inspect bounds & typesPrint or inspect collection sizes and verify variables are not empty or None/undefined.
2. Guard with safe accessUse optional chaining (?.), dictionary get(), or guard conditions before dereferencing.
3. Trace upstream originsFind where the value was instantiated or returned rather than only patching the symptom.

What does java.lang.ConcurrentModificationException mean?

Java collection iterators use a fail-fast mechanism backed by an internal modCount field. If modCount changes during iteration without going through the iterator's own methods, the iterator immediately throws ConcurrentModificationException to prevent corrupt iterations.

Why does java.lang.ConcurrentModificationException happen?

1. Calling list.remove() inside an enhanced for-loop

Enhanced for-loops (for (Item x : list)) use an implicit Iterator under the hood. Calling list.remove() bypasses the iterator, incrementing modCount and causing the next iterator.next() call to throw.

Breaks

for (String item : items) {
    if (item.isEmpty()) items.remove(item); // throws ConcurrentModificationException!
}

Works

items.removeIf(String::isEmpty); // safe modern Java collection removal

2. Modifying a shared collection across multiple threads without synchronization

Standard collections (ArrayList, HashMap) are not thread-safe. Concurrent writes corrupt iterator state.

Breaks

List<String> list = new ArrayList<>(); // accessed by multiple threads

Works

List<String> list = new CopyOnWriteArrayList<>();
// or Collections.synchronizedList(new ArrayList<>());

How to find it in your own code

Use Collection.removeIf(predicate) in Java 8+, or use an explicit Iterator and call iterator.remove(). For multi-threaded access, use concurrent collections like ConcurrentHashMap or CopyOnWriteArrayList.

Try it: fix this bug

medium

Exception in thread "main" java.util.ConcurrentModificationException

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void removeOddNumbers(List<Integer> numbers) {
        for (Integer number : numbers) {
            if (number % 2!= 0) {
                numbers.remove(number);
            }
        }
    }

    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>(List.of(1, 2, 3, 4));
        removeOddNumbers(numbers);
        System.out.println(numbers);
    }
}
Open with hintsRuns in your browser. No account needed.

More challenges with this error

Each of these challenges runs into this error or pattern. Find the bug and fix it in your browser — no install, no account needed.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Related error guides