Iterator vs ListIterator — how do you safely remove elements while iterating?
Reviewed by Gurusankar M.
Short Answer
Use the Iterator's own remove() (or removeIf) — never list.remove() inside a for-each, which throws ConcurrentModificationException. ListIterator adds backward traversal and set()/add() during iteration.
On this page
Concept Made Simple
Coffee Chat Question
“Iterator vs ListIterator — how do you safely remove elements while iterating?”
Remember It Faster
Mind Map Answer
- Iterator
- forward, hasNext/next/remove
- ListIterator
- bidirectional, set/add/remove
- Safe remove
- it.remove() or coll.removeIf(...)
Learn by Doing
Hands-on Keyboard
// BAD: throws ConcurrentModificationException
for (Order o : orders) if (o.isCancelled()) orders.remove(o);
// GOOD
orders.removeIf(Order::isCancelled);Think Beyond the Expected
What If?
Why does removing via the for-each loop throw but iterator.remove() doesn't?
The for-each uses an Iterator that tracks modCount. list.remove() bumps modCount behind the iterator's back → next() detects the mismatch and throws. iterator.remove() updates both the list and the iterator's expected modCount, so it stays consistent.
Real World
Filtering a collection in place (removing cancelled/expired items) is everyday code; removeIf is the clean, correct one-liner that avoids the classic CME crash.
Interviewer's Expectation
Keywords they're listening for:
- iterator.remove vs collection.remove
- removeIf
- modCount/fail-fast
- ListIterator bidirectional + set/add
Common Mistakes
- Calling list.remove() inside a for-each
- Modifying a Map's keySet while iterating without the iterator
- Assuming CME is guaranteed (it's best-effort)
Best Practices
- Prefer removeIf for conditional removal
- Use iterator.remove() when you need custom logic
- Iterate Map.entrySet() and use the iterator to remove
Follow-up Questions
- How does modCount drive fail-fast behavior?
- When would you use ListIterator.set() vs add()?
- How do you remove during iteration on a Map?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Iterators (Java Collections) Interview question: "Iterator vs ListIterator — how do you safely remove elements while iterating?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair