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:
- All implementations of java.util.Iterator
- All implementations of java.util.Enumeration
Now Let’s move towards the implementation of the Iterator Design Pattern.
Below is the complete source code:
Collection.java
1 2 3 4 5 6 7 8 9 10 11 |
package com.kkjavatutorials.collection; import com.kkjavatutorials.iterator.Iterator; /** * @author KK JavaTutorials *Contract to define Collection */ public interface Collection { public abstract Iterator getIterator(); } |
CollectionImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.kkjavatutorials.collection.impl; import com.kkjavatutorials.collection.Collection; import com.kkjavatutorials.iterator.Iterator; /** * @author KK JavaTutorials *Concrete implementation of Collection *which defined Itr(implements Iterator interface) *class as internal class * */ public class CollectionImpl implements Collection { String inputArr[] = {"Mark","Sean","John","KK","PK"}; @Override public Iterator getIterator() { return new Itr(); } /** * @author KK JavaTutorials *Concrete implementation of Iterator */ private class Itr implements Iterator{ int index =0; @Override public boolean hasNext() { if(index < inputArr.length) return true; return false; } @Override public Object next() { if(this.hasNext()) return inputArr[index++]; return null; } } } |
Iterator.java
1 2 3 4 5 6 7 8 9 10 |
package com.kkjavatutorials.iterator; /** * @author KK JavaTutorials *Contract to define Iterator */ public interface Iterator { public abstract boolean hasNext(); public abstract Object next(); } |
IteratorClientTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.kkjavatutorials.client; import com.kkjavatutorials.collection.Collection; import com.kkjavatutorials.collection.impl.CollectionImpl; import com.kkjavatutorials.iterator.Iterator; /** * @author KK JavaTutorials *Client program which uses *Iterator Design Pattern to *Iterate Over collection */ public class IteratorClientTest { public static void main(String[] args) { //Create an Instance of Collection Collection collection = new CollectionImpl(); //Getting instance of Collection out of collection Iterator iterator = collection.getIterator(); /*Now Visit every element of collection and print it's value using iterator*/ while (iterator.hasNext()) { Object object = iterator.next(); String name =(String)object; System.out.println("Name::"+name); } } } |
Output of this Program:
Name::Mark
Name::Sean
Name::John
Name::KK
Name::PK
You May Also Like:
- Interpreter Design Pattern in Java
- Memento Design Pattern in Java
- Visitor Design Pattern in Java
- State Design Pattern in Java
- Command Design Pattern in Java
- Observer Design Pattern in Java
- Strategy Design Pattern in java
- Mediator Design Pattern in Java
- Chain of Responsibility Design Pattern in Java
- Template Design Pattern in Java
- Bridge Design Pattern in Java.
- Flyweight Design Pattern in Java
- Composite Design Pattern in Java
- Proxy Design Pattern in Java
- Facade Design Pattern in Java
- Decorator Design Pattern in Java
- Adapter Design Pattern in Java
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.