Iterator Pattern in Java

Iterator Pattern

 
This pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. [GOF definition]
 
This pattern is used to get a way to access the elements of a collection object in sequential manner without any need to know its underlying representation.
 

Iterator Pattern Implementation

 
Iterator pattern relies on an interface called Iterator.

Here is one possible implementation of the Iterator interface:

iterator interface
 
Once we create an Iterator interface, we need to provide an implementation as well.

Similarly, we also need to create a Container interface and implementation which returns the iterator.

 
The following diagram depicts the relationship between these classes/interfaces :
 
iterator pattern
 

Create Iterator interface

package com.topjavatutorial.patterns.iterator;

public interface Iterator {
  public boolean hasNext();
  public Object next();
}


 

Create Iterator implementation

package com.topjavatutorial.patterns.iterator;

public class ColorIterator implements Iterator {
  
  String[] colors;
  int position = 0;
  
  public ColorIterator(String[] colors){
    this.colors = colors;
  }

  @Override
  public boolean hasNext() {
    if(position >= colors.length || colors[position] == null)
      return false;
    else
      return true;
  }

  @Override
  public String next() {
    String color = colors[position];
    position++;
    return color;
  }

}

 

Create Container interface

package com.topjavatutorial.patterns.iterator;

public interface IContainer {
  public Iterator createIterator();
}

 

Create Container implementation

package com.topjavatutorial.patterns.iterator;

public class ColorCollection implements IContainer {

  private String[] colors;
  
  public ColorCollection(){
    colors = new String[3];
    colors[0] = "Red";
    colors[1] = "Green";
    colors[2] = "Blue";
  }
  
  @Override
  public Iterator createIterator() {
    return new ColorIterator(colors);
  }

}

 

Testing Iterator pattern implementation

package com.topjavatutorial.patterns.iterator;

public class IteratorPatternDemo {

  public static void main(String[] args) {
    ColorCollection colorCollection = new ColorCollection();
    Iterator iter = colorCollection.createIterator();
    
    while(iter.hasNext()){
      System.out.println(iter.next());
    }
  }

}


 

Output

Red
Green
Blue

 

Iterator pattern in JDK

 
Since JDK 5, Java provides an Iterator interface.

public interface Iterator<E> {
    boolean hasNext();
    E next();
    void remove();
}

 
We can use Iterator to iterate through Collections.
 
 
Here are some articles that you may like on this :

Iterating Collection using Iterator, ListIterator, Spliterator
 
Java Collections
 
 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

You may also like...

Leave a Reply.. code can be added in <code> </code> tags