Simple definition#
The Iterator pattern gives a way to visit the elements of a collection one at a time, without exposing how the collection is stored: an array, a linked list, a tree, a database cursor or pages from a remote API.
GoF intent: “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” It is also known as Cursor.
The problem#
A CRM API returns customers in pages of 100 with a nextCursor token. Every piece of code that needs “all customers” writes the same loop: call, process, read the cursor, call again, stop when the cursor is null. The paging protocol leaks into business logic, gets copied with small bugs, and fetches pages eagerly even when the caller only needs the first few results.
Real-world analogy#
A TV remote’s “next channel” button. You move through channels one by one without knowing whether they arrive by cable, satellite or the internet.
Structure#
- Iterator (
java.util.Iterator) —hasNext()andnext(). - Concrete iterator — the anonymous class in
AllCustomers.iterator(), which keeps traversal state (current page, cursor). - Aggregate (
java.lang.Iterable) — creates iterators. - Concrete aggregate (
AllCustomers).
classDiagram
direction LR
class Iterable~T~ {
<<interface>>
+iterator() Iterator~T~
}
class Iterator~T~ {
<<interface>>
+hasNext() boolean
+next() T
}
class AllCustomers {
-CustomerApi api
-int pageSize
+iterator() Iterator~String~
+stream() Stream~String~
}
class PagingIterator {
-Iterator current
-String cursor
}
Iterable <|.. AllCustomers
Iterator <|.. PagingIterator
AllCustomers ..> PagingIterator : creates
PagingIterator --> CustomerApi : fetches pages lazilyJava implementation#
The code is in examples/java/src/hub/patterns/iterator/IteratorDemo.java.
/** Aggregate: hides the paging protocol behind Iterable, so clients just loop. */
final class AllCustomers implements Iterable<String> {
private final CustomerApi api;
private final int pageSize;
AllCustomers(CustomerApi api, int pageSize) {
this.api = api;
this.pageSize = pageSize;
}
@Override public Iterator<String> iterator() {
return new Iterator<>() { // concrete iterator: one per traversal
private Iterator<String> current = List.<String>of().iterator();
private String cursor = ""; // "" = first page, null = no more pages
@Override public boolean hasNext() {
while (!current.hasNext() && cursor != null) { // fetch lazily, only when needed
Page<String> page = api.fetch(cursor, pageSize);
current = page.items().iterator();
cursor = page.nextCursor();
}
return current.hasNext();
}
@Override public String next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return current.next();
}
};
}
Stream<String> stream() {
return StreamSupport.stream(spliterator(), false);
}
}for-each over every page:
[fetch page starting at 0]
customer-1
customer-2
customer-3
[fetch page starting at 3]
customer-4
customer-5
customer-6
[fetch page starting at 6]
customer-7
Stream, first 2 only (one page fetched):
[fetch page starting at 0]
customer-1
customer-2The for loop knows nothing about pages. Pages are fetched lazily, only when the current one is exhausted, so the stream that takes two customers fetches one page.
Step-by-step walkthrough#
- Implement
Iterable<T>so the aggregate works with the enhancedforloop. - Return a new iterator per call. Each traversal keeps its own state; two loops over the same aggregate do not interfere.
- Make
hasNext()idempotent. It may be called many times; it fetches only when the buffer is empty. Thewhileloop also handles empty pages. next()throwsNoSuchElementExceptionwhen there are no more elements, as the contract requires.- Get streams for free with
StreamSupport.stream(spliterator(), false): the defaultspliterator()wraps the iterator.
External versus internal iteration#
| External iteration | Internal iteration | |
|---|---|---|
| Who controls the loop | The client (for, hasNext/next) | The collection (forEach, streams) |
| Early exit, interleaving two collections | Easy | Needs operators such as takeWhile, limit, zip |
| Parallelism | Manual | parallelStream() via Spliterator |
| Java API | Iterator, ListIterator | Iterable.forEach, Stream |
GoF described external iterators; Java 8 streams added internal iteration. Both are the Iterator pattern. See Lambdas and Streams.
Fail-fast and weakly consistent iterators#
- Fail-fast (
ArrayList,HashMap): if the collection is structurally modified during iteration other than through the iterator’s ownremove(), the next call throwsConcurrentModificationException. It is a best-effort bug detector, not a concurrency guarantee. - Weakly consistent (
ConcurrentHashMap,ConcurrentLinkedQueue): never throw; they may or may not reflect changes made after the iterator was created. - Snapshot (
CopyOnWriteArrayList): iterate over the array as it was when the iterator was created.
for (String s : names) {
if (s.isBlank()) names.remove(s); // ConcurrentModificationException
}
names.removeIf(String::isBlank); // correct
See The Java Collections Framework.
Advantages and disadvantages#
| Advantages | Disadvantages |
|---|---|
| Clients do not depend on the collection’s internal structure | Overkill for simple arrays and lists that already have iterators |
| Several traversals can run at once | An iterator over a mutable collection can be invalidated |
| Lazy traversal of large or remote data | Hidden I/O inside hasNext() can surprise callers (document it) |
| Uniform loops over different structures (SRP: traversal logic is separate) | — |
When to use it#
- A custom data structure (tree, graph, ring buffer) needs to be traversed.
- Large, lazy or remote data: paged APIs, database cursors, file lines (
Files.lines). - Several traversal orders over the same structure (in-order, breadth-first).
When not to use it#
- A standard collection already provides what you need.
- You need random access by index; iterators are sequential.
Common mistakes#
- Returning the same iterator instance from every
iterator()call. hasNext()with side effects that advance the position.- Leaking resources: an iterator over a database cursor or file must be closed; prefer
Streamwith try-with-resources.
Real-world uses#
java.util.Iterator,ListIterator,Spliterator,Enumeration(legacy).java.util.Scanner,BufferedReader.lines(),Files.lines,DirectoryStream.- JDBC
ResultSetis a cursor-style iterator. - Spring Data’s
Stream<T>repository methods andSlice/PagewithPageable; AWS SDK paginators.
Interview questions and answers#
What is the Iterator pattern? A behavioral pattern that provides sequential access to a collection’s elements without exposing its internal representation.
What is the difference between Iterator and Iterable?
Iterable is the aggregate: it creates iterators and enables the for-each loop. Iterator is the cursor that keeps traversal state.
What is a fail-fast iterator?
One that throws ConcurrentModificationException if the collection is structurally modified during iteration, except through the iterator itself.
External versus internal iteration?
External: the client drives the loop with hasNext/next. Internal: the collection drives it and calls back the client, as in forEach and streams.
Summary#
- Iterator separates traversal from storage.
Iterablecreates iterators; each iterator holds its own state.- Iterators can be lazy, which suits paged and remote data.
- Know fail-fast versus weakly consistent behaviour in the JDK.
Related topics#
- The Java Collections Framework
- Lambdas, Method References and Streams
- Composite Pattern — iterating trees
References#
- Gamma, Helm, Johnson, Vlissides. Design Patterns. Addison-Wesley, 1994. Iterator, p. 257.
- Eric Freeman, Elisabeth Robson. Head First Design Patterns (2nd edition). O’Reilly, 2020. Chapter 9 (diner and pancake-house menus).
java.util.Iterator, Java SE 21 API.