How to avoid ConcurrentModificationException while removing elements from `ArrayList` while iterating it?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
ConcurrentModificationException usually appears when Java code modifies a collection structurally while iterating over it with a fail-fast iterator. When the goal is to remove elements from an ArrayList during iteration, the safe pattern is to remove through the iterator itself or to restructure the operation so the list is not modified mid-loop in an unsafe way.
Why the Exception Happens
A for-each loop uses an iterator under the hood. If you remove elements from the list directly while that iterator is active, Java detects the structural change and throws ConcurrentModificationException.
That fails because the list is modified outside the iterator that is performing the traversal.
Safe Option 1: Use an Explicit Iterator
The standard safe solution is to use an explicit iterator and call remove() on that iterator.
This works because the iterator updates its own internal modification state correctly.
Safe Option 2: Use removeIf
If the goal is simply to remove every element matching a predicate, modern Java gives you a cleaner option.
For many cases, removeIf is the most readable answer.
Safe Option 3: Collect Then Remove
Another pattern is to collect the elements to remove first and then remove them afterward.
This can be useful when the removal rule is complex or when you want to inspect the removed elements separately.
What About CopyOnWriteArrayList?
CopyOnWriteArrayList avoids this exception because iteration happens over a snapshot, not over a mutable live view. But it is not a drop-in answer for ordinary ArrayList logic. It has different performance tradeoffs and is mainly useful for read-heavy concurrent scenarios.
If the code is single-threaded and simply wants safe removal while iterating, a normal iterator or removeIf is usually better.
Choose the Simplest Safe Pattern
As a practical rule:
- use
iterator.remove()when you are already iterating manually, - use
removeIfwhen the logic is just predicate-based removal, - use collect-then-remove when the removal rule or bookkeeping needs more structure.
That keeps the intent of the code visible instead of turning collection mutation into a side effect hidden inside a loop.
Common Pitfalls
A common mistake is assuming the exception means several threads are involved. It often happens in single-threaded code too, because the issue is iterator safety, not necessarily real concurrency.
Another issue is removing directly from the list inside a for-each loop. That is the classic fail-fast pattern.
Developers also sometimes reach for CopyOnWriteArrayList too quickly when the real need is simply to use the iterator's own remove method.
Summary
- '
ConcurrentModificationExceptionusually means a fail-fast iterator saw an unsafe structural change.' - Use
iterator.remove()when removing during manual iteration. - Use
removeIfwhen the goal is predicate-based removal. - Collect elements first and remove later if the logic needs extra structure.
- Do not modify an
ArrayListdirectly from inside a for-each loop.
Related reading
- How to avoid installing Unlimited Strength JCE policy files when deploying an application?
- How to avoid java.util.ConcurrentModificationException when iterating through and removing elements from an ArrayList
- How to avoid Not on FX application thread; currentThread JavaFX Application Thread error?
- How to avoid Sharing is only supported for boot loader classes because bootstrap classpath has been appended warning during debug with Java 11?
- How to avoid RuntimeError dictionary changed size during iteration error?
- How to avoid the out of range error using shuffle_batch function?
- How to avoid the need to specify the WSDL location in a CXF or JAX-WS generated webservice client?
- How to bind an object list with thymeleaf?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.