Skip to content
StackDecode
System Design
15. Software Design PatternsAnimated walkthrough

Iterator Pattern

Walk through a collection without exposing it.

The idea

An iterator offers next and has-next across any structure.

Watch it work

The steps light up one at a time. Use the controls to pause or step through.

  1. 1Ask the collection for an iterator
  2. 2Call next
  3. 3Process each item
  4. 4Stop at the end
1/4

Step by step

  1. 1Ask the collection for an iterator
  2. 2Call next
  3. 3Process each item
  4. 4Stop at the end

Where it is used

Looping over structures.

Watch out for

Modifying the collection while iterating.

Related