Iterator Design Pattern in Java

Iterator pattern falls under behavioral design pattern. Iterator pattern is very commonly used design pattern in Java. This pattern is used to iterate through a collection of objects without exposing its underlying representation. It is used to access the elements of an aggregate object sequentially.

For example, Java’s collections like ArrayList and HashMap have implemented the iterator pattern.

Class Diagram

Iterator Pattern in Java

Implementation

We can write our own iterator.

Create an interface with below operations

Create another interface to get the above iterator

Now we create a class which holds some designations through which we will be iterating and check whether the array element has next element from its current position.

We create a test class which will iterate through the collection of objects and display

Output 

That’s all. Thanks for your reading.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.