The Iterator Design Pattern — Walking Through Collections the Smart Way
Ever needed to loop through a collection but didn’t want to expose its internal structure? 🤔 That’s where the Iterator Design Pattern…
The Iterator Design Pattern — Walking Through Collections the Smart Way

Ever needed to loop through a collection but didn’t want to expose its internal structure? 🤔 That’s where the Iterator Design Pattern shines!
It provides a standard way to access elements in a collection, without revealing how that collection is actually stored or managed.
Think of it like a TV remote — you can flip channels forward or backward, but you don’t need to know how the TV’s internal wiring works. 📺
Real-World Example — Playlist of Songs
Imagine you’re building a Music Player App with a playlist of songs. You want to go through the songs one by one, but what if your playlist storage changes later (from an array to a list or database)? You don’t want to rewrite your looping logic every time you need to update it.
Without the Iterator Pattern
import java.util.ArrayList;
import java.util.List;
public class Playlist {
private List<String> songs = new ArrayList<>();
public void addSong(String song) {
songs.add(song);
}
public List<String> getSongs() {
return songs;
}
}
public class Main {
public static void main(String[] args) {
Playlist playlist = new Playlist();
playlist.addSong("Shape of You");
playlist.addSong("Believer");
playlist.addSong("Counting Stars");
// Client directly accesses collection
for (int i = 0; i < playlist.getSongs().size(); i++) {
System.out.println("Playing: " + playlist.getSongs().get(i));
}
}
}
This works fine — but notice how the client depends on how songs are stored (List<String>). If we later change List to a Set Or, in the case of an array, we have to update every piece of code that loops over the songs.
With the Iterator Pattern
Let’s decouple our looping logic from how the collection is stored.
Step 1: Create the Iterator interface
public interface Iterator {
boolean hasNext();
Object next();
}
Step 2: Create a Collection interface
public interface SongCollection {
Iterator createIterator();
}
Step 3: Implement a concrete collection and an iterator
import java.util.ArrayList;
import java.util.List;
public class Playlist implements SongCollection {
private List<String> songs = new ArrayList<>();
public void addSong(String song) {
songs.add(song);
}
public Iterator createIterator() {
return new SongIterator(songs);
}
}
public class SongIterator implements Iterator {
private List<String> songs;
private int index = 0;
public SongIterator(List<String> songs) {
this.songs = songs;
}
public boolean hasNext() {
return index < songs.size();
}
public Object next() {
return songs.get(index++);
}
}
Step 4: Use the Iterator
public class Main {
public static void main(String[] args) {
Playlist playlist = new Playlist();
playlist.addSong("Shape of You");
playlist.addSong("Believer");
playlist.addSong("Counting Stars");
Iterator iterator = playlist.createIterator();
while (iterator.hasNext()) {
System.out.println("Playing: " + iterator.next());
}
}
}
Now, your client code doesn’t care how songs are stored — It just uses the iterator to navigate them safely and consistently.
Why the Iterator Pattern Rocks
- Hides collection details (array, list, set, etc.)
- Makes iteration consistent and clean
- Supports multiple traversal strategies
- Simplifies client code
Real-World Uses
- Java’s
Iteratorinterface — used inList,Set, and other collections - Python iterators & generators —
for ... insyntax - Database cursors — iterating over query results
- Custom data structures — providing traversal without exposing internals
Quick Recap
- Problem: Client code depends on the collection structure
- Solution: Create an iterator to traverse elements without exposing the structure
- Result: Clean, flexible, and decoupled iteration logic
Next time you’re building a collection or data structure, add an Iterator — and make traversal effortless and elegant.
메타데이터
- post_id
- 263d4f0dac43
- slug
- the-iterator-design-pattern-walking-through-collections-the-smart-way-263d4f0dac43
- url
- https://medium.com/@dhanushprabakaran/the-iterator-design-pattern-walking-through-collections-the-smart-way-263d4f0dac43
- canonical_url
- https://medium.com/@dhanushprabakaran/the-iterator-design-pattern-walking-through-collections-the-smart-way-263d4f0dac43
- author_url
- https://medium.com/@dhanushprabakaran
- status
- ok
- fetched_at
- 2026-08-04 02:06:22