ConcurrentModificationException ☕ Java

java.util.ConcurrentModificationException

A collection was modified while being iterated (even in a single thread).

Seen on: Java

Meaning

Java’s fail-fast iterators throw when the underlying list/map changes structurally during iteration — most often by calling list.remove() inside a for-each loop.

Common causes

  • Removing/adding inside a for-each loop
  • Another thread modifying the collection
  • Modifying a map while iterating its keySet/entrySet

⚡ Quick fix

  1. Use iterator.remove() or list.removeIf(...)
  2. Iterate over a copy
  3. Use concurrent collections for multi-threaded access

Detailed fix by platform

Java

  1. removeIf:
    java
    users.removeIf(u -> !u.isActive());

How to diagnose

  1. Loop — Is the collection modified inside its own loop?
  2. Threads — Shared between threads?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.