How to remove a key from HashMap while iterating over it?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
If you remove entries from a HashMap by calling map.remove(key) while iterating with a normal loop, Java will usually throw ConcurrentModificationException. The safe rule is simple: if you are iterating with an Iterator, remove through that same iterator. In modern Java, removeIf on the entry set is also a clean option for predicate-based removal.
Why map.remove(...) Fails During Iteration
A HashMap iterator is fail-fast. It keeps track of structural modifications to the map. If the map changes unexpectedly while the iterator is still in use, the iterator detects the mismatch and throws.
That is why this pattern is unsafe:
The enhanced for loop is using an iterator behind the scenes. Calling map.remove changes the map outside the iterator's own removal path.
The Correct Classic Solution: Iterator.remove()
Use an explicit iterator and remove through it.
This works because the iterator updates its own internal state when remove() is called.
Two details matter:
- call
next()beforeremove() - call
remove()at most once per returned entry
If you violate those rules, you get IllegalStateException.
Java 8 and Later: removeIf
If your logic is a pure filter, removeIf is often cleaner.
This is concise and readable, especially when the condition is simple.
When You Need More Control
Sometimes you should not remove during iteration at all. If the removal logic is complicated, or if you need to inspect the map in several passes, collecting keys first can make the code clearer.
This uses extra memory, but it avoids modifying the map during the first traversal.
What About ConcurrentHashMap?
ConcurrentHashMap is for concurrent access, but it does not exist just to avoid learning iterator rules. If your code is single-threaded and the only issue is safe removal during iteration, Iterator.remove or removeIf is still the right answer.
Use ConcurrentHashMap only when you actually need concurrent behavior and understand its iteration semantics.
Common Pitfalls
A common mistake is assuming that removing by key is safe because you are "only deleting the current item". For a fail-fast iterator, that is still an external structural modification.
Another mistake is calling iterator.remove() twice without calling next() again. That triggers IllegalStateException.
People also sometimes choose ConcurrentHashMap to silence the exception instead of fixing the iteration logic.
Finally, if the logic is really filtering, removeIf is clearer than hand-written iterator code in modern Java.
Summary
- Do not call
map.remove(key)while iterating aHashMapwith a normal iterator or enhancedforloop - The safe classic solution is
Iterator.remove()on the iterator you are currently using - In Java 8 and later,
map.entrySet().removeIf(...)is often the cleanest choice - For more complex logic, collect keys first and remove them afterward
- '
ConcurrentHashMapis not the default fix for single-threaded iteration problems' - The key idea is to modify the map in a way the iteration mechanism expects
Related reading
- How to remove all duplicates from an array of objects?
- How to remove an edge from a half edge structure?
- How to remove an element from a doubly-nested array in a MongoDB document
- How to remove an element from a list by index
- How to remove all callbacks from a Handler?
- How to remove line breaks from a file in Java?
- How to remove an element from an array in Swift
- How to remove duplicate strings from an array in Kotlin

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.