Iterator vs ListIterator — Traversal and Modification
Fast recall
Quick revision
Core answer
Collection; ListIterator is a richer interface available only for `List` that supports bidirectional traversal, element modification (set, add), and index access (nextIndex, previousIndex). Iterator has hasNext(), next(), and remove() (optional); ListIterator adds hasPrevious(), previous(), set(E), add(E), nextIndex(), previousIndex(). Use Iterator for general traversal of any collection (Set, Map values, etc.); use ListIterator when you need to modify a List in place during iteration, walk backward, or know the current index. Both are fail-fast in the standard List implementations (ArrayList, LinkedList) — they throw ConcurrentModificationException if the underlying list is modified externally during iteration. Modern Java (8+): enhanced for-loop (for (T t : collection)) replaces most explicit Iterator usage; use removeIf(predicate) or Streams for filter-and-collect operations instead of explicit iteration. Common bug: calling list.remove(item) inside an enhanced for-loop throws ConcurrentModificationException — must use iterator.remove() or removeIf() instead. For interviews: explain the forward-vs-bidirectional distinction, mention set()/add() for in-place modification, and address the fail-fast contract.Understanding of bidirectional traversal, modification capabilities, and when ListIterator provides value over Iterator
Not knowing that ListIterator can traverse backwards and add/set elements during iteration, or confusing Iterator.remove() with List.remove()
Show that ListIterator.add() inserts before the implicit cursor position and that nextIndex()/previousIndex() reveal the cursor location without advancing it
Interview-ready guide
Interview answer
Iterator is forward-only for any Collection; ListIterator extends it with bidirectional traversal and in-place modification — available only on List implementations.
Iterator:
hasNext(),next(),remove()— remove deletes the last element returned bynext()- Works on any Collection: ArrayList, HashSet, ArrayDeque
ListIterator:
- Bidirectional:
previous(),hasPrevious()— traverse in either direction - In-place mutation:
set()replaces the last element returned;add()inserts before the current cursor position - Index queries:
nextIndex(),previousIndex()
Cursor model gotcha:
- After
next()returns element A,add(X)places X after A — cursor advances past X automatically set()without a precedingnext()orprevious(), or called afteradd()/remove(), throwsIllegalStateException
I recommend Iterator for simple forward traversal and ListIterator when you need to insert, replace, or traverse backwards during iteration.
Iterator vs ListIterator — Traversal and Modification
Direct Answer
Iterator is the basic forward-only traversal interface for any Collection; ListIterator is a richer interface available only for List that supports bidirectional traversal, element modification (set, add), and index access (nextIndex, previousIndex). Iterator has hasNext(), next(), and remove() (optional); ListIterator adds hasPrevious(), previous(), set(E), add(E), nextIndex(), previousIndex(). Use Iterator for general traversal of any collection (Set, Map values, etc.); use ListIterator when you need to modify a List in place during iteration, walk backward, or know the current index. Both are fail-fast in the standard List implementations (ArrayList, LinkedList) — they throw ConcurrentModificationException if the underlying list is modified externally during iteration. Modern Java (8+): enhanced for-loop (for (T t : collection)) replaces most explicit Iterator usage; use removeIf(predicate) or Streams for filter-and-collect operations instead of explicit iteration. Common bug: calling list.remove(item) inside an enhanced for-loop throws ConcurrentModificationException — must use iterator.remove() or removeIf() instead. For interviews: explain the forward-vs-bidirectional distinction, mention set()/add() for in-place modification, and address the fail-fast contract.
Common Mistakes & Pitfalls
Not knowing that ListIterator can traverse backwards and add/set elements during iteration, or confusing Iterator.remove() with List.remove()
Check your understanding
Answer these without looking back at the article.
- When would you use ListIterator instead of Iterator?
- Why does removing an item with `list.remove()` inside a for-each throw ConcurrentModificationException?
- What's the difference between `iterator.remove()` and `list.remove(item)`?
- Can you use ListIterator on a Set?
- How do you safely remove items while iterating?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.