Iterating Collections
Fast recall
Quick revision
Core answer
Iterator, a ListIterator for list-specific backward traversal and edits, and Iterable.forEach() with a lambda. Use a for-each loop for a clear read-only pass. Use an iterator when you need cursor control; call remove() only if that iterator supports the optional operation. For simple filtering, removeIf() is often clearer.Understanding of Collection API iteration mechanisms and knowing when each approach is appropriate for different scenarios
Assuming every iterator supports removal, or changing a fail-fast collection directly while its iterator is active
Explain that Iterator.remove is optional, arrays use separate enhanced-for rules, and concurrent collections document snapshot or weakly consistent iteration
Interview-ready guide
Interview answer
Java collections can be traversed with a for-each loop, an explicit Iterator, a ListIterator, or the forEach method. I use for-each for the common case because it is easy to read. If I need cursor control or removal during the pass, I use an iterator and call its remove() only when the iterator supports that optional operation. For a simple filter, removeIf() is usually shorter.
ListIterator is available only for lists and can move in both directions, report indexes, replace the last returned element, or insert at the cursor. forEach accepts a lambda and is useful for a small action, but it does not provide the normal loop statements such as break and continue. I would not describe every iterator as fail-fast or as using modCount; those are documented behaviors and implementation details of particular collection types.
Iterating Collections
Direct Answer
Common choices are a for-each loop, an explicit Iterator, a ListIterator for list-specific backward traversal and edits, and Iterable.forEach() with a lambda. Use a for-each loop for a clear read-only pass. Use an iterator when you need cursor control; call remove() only if that iterator supports the optional operation. For simple filtering, removeIf() is often clearer.
Code Example
Here are all four styles in one method, each suited to a different need.
import java.util.*;
public class OrderProcessor {
public void processOrders(List<Order> orders) {
// 1. for-each — clean, read-only access
for (Order order : orders) {
System.out.println(order.id());
}
// 2. Iterator — safe removal during iteration
Iterator<Order> it = orders.iterator();
while (it.hasNext()) {
Order order = it.next();
if (order.amount() < 10) {
it.remove(); // the iterator-coordinated removal when supported
}
}
// 3. ListIterator — bidirectional traversal
ListIterator<Order> back = orders.listIterator(orders.size());
while (back.hasPrevious()) {
System.out.println(back.previous().item());
}
// 4. forEach with a lambda — functional pass
orders.forEach(order -> System.out.println(order.customer()));
}
}
record Order(Long id, String item, String customer, double amount) {}
The for-each and forEach read cleanly, the Iterator deletes safely, and the ListIterator walks backward — one collection, four tools.
Common Mistakes & Pitfalls
- Changing the collection behind an iterator: many standard mutable collections may throw
ConcurrentModificationException. Use the operation supported by the iterator or choose another filtering approach. - Assuming
Iterator.remove()always works: it is an optional operation and may throwUnsupportedOperationException, especially for unmodifiable data. - Calling
next()without checking: when no element remains,next()throwsNoSuchElementException. - Forcing a lambda:
forEach()has no normal loopbreakorcontinue; use a loop when control flow matters.
Check your understanding
Answer these without looking back at the article.
- What causes ConcurrentModificationException?
- How do you remove elements from a List while iterating?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.