Remove elements from collection while iterating
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Iterating over a collection and modifying its contents during iteration is a common task in software development. However, this process can be inherently problematic as modifications such as removing elements can disrupt the iterator and potentially lead to errors or unexpected behavior. This article explains the challenges and solutions associated with removing elements from a collection while iterating through it, using Java as the primary example.
The Problem
The primary issue when trying to remove items from a collection during iteration in Java is the ConcurrentModificationException. This exception is thrown when an attempt is made to modify a collection while it is being iterated, except through the iterator's own remove method. This situation arises because most iterators are fail-fast, detecting any modification to the collection structure that it wasn't expecting.
Solutions
1. Using Iterator's remove() Method
The safest and most common method to remove elements from a collection during iteration is through the Iterator’s own remove() method. This method ensures that the collection's size is adjusted, and the iterator's state is valid after the removal. Here’s an example using Java:
In this example, "Banana" is safely removed from the list without causing a ConcurrentModificationException.
2. Using Concurrent Collections
If the collection is intended to be accessed and modified by multiple threads, using concurrent collections like CopyOnWriteArrayList or ConcurrentHashMap might be preferable. These collections have thread-safe iterators that handle modifications by making fresh copies of the underlying data structures.
Example using CopyOnWriteArrayList:
3. Collecting Items to Remove Later
Another safe approach is first to gather items that need to be removed in a separate collection and then remove these outside the loop. This method is straightforward and works with all types of collections:
Summary Table
| Method | Collection Type | Thread Safe | Notes |
Iterator’s remove() | General purpose | No | Safest for single-thread, direct modification |
| Concurrent Collection Methods | Concurrent collections | Yes | Handles multi-threading scenarios |
| Collect and Remove After Iteration | General purpose | No | Simple and effective, but requires additional space |
Additional Considerations
- Performance: Using concurrent collections or creating a separate list for items to remove can have performance implications due to the overhead of managing the additional complexity or data structures.
- Best Practices: Favor immutability where possible; an immutable data structure or an effectively immutable pattern can sidestep many of these iteration issues.
- Library Support: Some libraries like Apache Commons and Google Guava provide additional collection utilities that can simplify common tasks including safely removing items.
In summary, removing elements from a collection while iterating requires careful handling to avoid errors and ensure consistent behavior. Java provides multiple ways to handle this, each suitable for different scenarios, ensuring that developers can choose the best strategy based on their specific requirements.
Related reading
- remove elements from link list whose sum equals to zero
- Remove empty array elements
- Remove empty elements from an array in Javascript
- Remove empty strings from a list of strings
- Remove last character of a StringBuilder?
- Remove multiple keys from Map in efficient way?
- Remove item from list based on condition
- Remove Item in Dictionary based on Value

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.