Sitemap
shubham mishra

shubham mishra

Hi there! I'm a full-stack developer with 6 years of experience in building robust, scalable, and maintainable web applications.

Iterator Design Pattern

2 min readMay 29, 2025

--

The Iterator Pattern is a design pattern that helps you go through the elements of a collection (like a list or array) one by one, without knowing how the collection is built.

Get shubham mishra’s stories in your inbox

Join Medium for free to get updates from this writer.

Think of it like flipping through a photo album — you don’t need to know how the album is made; you just turn the pages.

📖 Real-Life Example

Imagine you have a playlist of songs. You can play the next song or go back to the previous one. You don’t need to know how the songs are stored inside — you just use the “Next” and “Previous” buttons. That’s the iterator in action!

💡 Why Use It?

  • You can loop through a collection without exposing its details.
  • It works for different types of collections — arrays, lists, sets, etc.
  • You can create your own custom way to move through data.

🧱 Key Concepts

  • Iterator: Provides a way to access elements sequentially.
  • Aggregate/Collection: Stores the objects and creates the iterator.

💻 Implementation (Custom)

  1. Iterator Interface
public interface Iterator<T> {
boolean hasNext();
T next();
}

2. Aggregate Interface

public interface Collection<T> {
Iterator<T> createIterator();
}

3. Concrete Collection

public class NameRepository implements Collection<String> {
private String[] names = {"Alice", "Bob", "Charlie"};

@Override
public Iterator<String> createIterator() {
return new NameIterator();
}

private class NameIterator implements Iterator<String> {
private int index = 0;

@Override
public boolean hasNext() {
return index < names.length;
}

@Override
public String next() {
return hasNext() ? names[index++] : null;
}
}
}

4. Client

public class Main {
public static void main(String[] args) {
Collection<String> names = new NameRepository();
Iterator<String> iterator = names.createIterator();

while (iterator.hasNext()) {
System.out.println("Name: " + iterator.next());
}
}
}

✅ Pros and ❌ Cons

Pros:

  • Encapsulates iteration logic.
  • Promotes single responsibility (collection doesn’t need to manage traversal).
  • Allows different traversal strategies.

Cons:

  • Adds complexity for simple collections.
  • Requires additional classes for each new type of iteration.

🏁 Conclusion

The Iterator Pattern separates collection traversal from the collection itself. This decouples how we store data from how we access it, which makes code more maintainable and flexible. Whether you’re using Java’s built-in collections or creating your own, understanding this pattern is crucial.

📚 GitHub Repository for Reference:
👉 https://github.com/iamshubhammishra/java-design-patterns/tree/main/Iterator

--

--

shubham mishra
shubham mishra

Written by shubham mishra

Hi there! I'm a full-stack developer with 6 years of experience in building robust, scalable, and maintainable web applications.