Iterator Design Pattern in Java

By | July 8, 2020

In this post, We will talk and learn about the Iterator Design Pattern in Java.

Key Points About Iterator Design Pattern :

  • Iterator design pattern falls under behavioral patterns.
  • This pattern is mainly used to provide a standard way to traverse through a group of Objects. Iterator pattern is widely used in the Java Collection framework. Iterator interface provides methods for traversing through a collection Object.
  • This pattern provides a way to access the elements of an aggregate object without exposing its underlying representation.
  • Iterator pattern is not only about traversing through a collection, we can provide different kind of iterators based on our requirements.
  • Iterator design pattern hides the actual implementation of traversal through the collection and client programs just use Iterator methods.
  • Iterator pattern is useful when you want to provide a standard way to iterate over a collection and hide the implementation logic from the client program.
  • The logic for iteration is embedded in the collection class itself and it helps the client program to iterate over them easily.

Iterator Design Pattern in JDK:

Now Let’s move towards the implementation of the Iterator Design Pattern.

Below is the complete source code:

Iterator Design Pattern

Collection.java

 

CollectionImpl.java

 

Iterator.java

 

IteratorClientTest.java

Output of this Program:

Name::Mark
Name::Sean
Name::John
Name::KK
Name::PK

You May Also Like:

That’s all about the Iterator Design Pattern in Java.
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published.