← All patterns
3 / 5
Iterators & Streams

Iterator Implementation

🔁
Picture this

A ticket window reveals exactly one ticket per pull while the stack behind the glass stays hidden.

When you see it

Signal words

If the prompt uses any of these, this pattern should come to mind first.

iteratorhasNextnextflattenlazygenerator

Approach

Keep the traversal state private. hasNext should be idempotent. next should either return one value and advance or throw when exhausted.

Skeleton

class ArrayIterator<T> {
  private index = 0;

  constructor(private readonly items: T[]) {}

  hasNext(): boolean {
    return this.index < this.items.length;
  }

  next(): T {
    if (!this.hasNext()) throw new Error("Iterator exhausted");
    return this.items[this.index++];
  }
}

Watch for

Calling hasNext multiple times must not consume values.

Define exhaustion behavior before coding.