EasyExperience level: 0-2 years · 3-5 years1 min read

Iterator vs ListIterator — how do you safely remove elements while iterating?

Reviewed by Gurusankar M.

Asked inTCSInfosysAccentureDeloitte
#iterator#listiterator#removeif#concurrentmodification
Report issue

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

java
// 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

  1. How does modCount drive fail-fast behavior?
  2. When would you use ListIterator.set() vs add()?
  3. 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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.