Iterator pattern is used to traverse and iterate through a list of items.
This article demonstrates Iterator pattern implementations in Java. Check the following examples.
Examples
Check the following examples of the Iterator pattern implementation in Java-
Example #1: Pagination Iterator
Let’s take the example of storing and showing pagination items.
Page Item Class [General Class]
// Page.java
package com.bigboxcode.designpattern.iterator.pagination;
public class Page {
private int number;
private String path;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getPath() {
if (path == null) {
return "/page/" + number;
}
return path;
}
public void setPath(String path) {
this.path = path;
}
}
Page List Interface
// AbstractPageList.java
package com.bigboxcode.designpattern.iterator.pagination;
public interface AbstractPageList {
void add(Page page);
void remove(Page page);
AbstractIterator iterator();
}
Page List Class
// PageList.java
package com.bigboxcode.designpattern.iterator.pagination;
import java.util.ArrayList;
import java.util.List;
public class PageList implements AbstractPageList {
private List<Page> pages = new ArrayList<>();
@Override
public void add(Page page) {
pages.add(page);
}
@Override
public void remove(Page page) {
pages.remove(page);
}
@Override
public AbstractIterator iterator() {
return new Iterator(pages);
}
}
Iterator Interface
// AbstractIterator.java
package com.bigboxcode.designpattern.iterator.pagination;
public interface AbstractIterator {
boolean hasNext();
Page next();
}
Iterator Class
//Iterator.java
package com.bigboxcode.designpattern.iterator.pagination;
import java.util.List;
public class Iterator implements AbstractIterator {
private int currentPosition = 0;
private final List<Page> pages;
public Iterator(List<Page> pages) {
this.pages = pages;
}
@Override
public boolean hasNext() {
return currentPosition < pages.size();
}
@Override
public Page next() {
Page page = pages.get(currentPosition);
currentPosition++;
return page;
}
}
Demo
// Demo.java
package com.bigboxcode.designpattern.iterator.pagination;
public class Demo {
public static void main(String[] args) {
PageList pageList = populatePageList();
AbstractIterator paginator = pageList.iterator();
while(paginator.hasNext()) {
Page page = paginator.next();
System.out.println("Page Number: " + page.getNumber());
System.out.println("Page Path: " + page.getPath());
System.out.println("----------------------------------------");
}
}
// Dummy function to populate the pagination list
private static PageList populatePageList() {
PageList pageList = new PageList();
for (int i = 0; i < 10; i++) {
Page page = new Page();
page.setNumber(i);
pageList.add(page);
}
return pageList;
}
}
Output
Page Number: 0
Page Path: /page/0
----------------------------------------
Page Number: 1
Page Path: /page/1
----------------------------------------
Page Number: 2
Page Path: /page/2
----------------------------------------
Page Number: 3
Page Path: /page/3
----------------------------------------
Page Number: 4
Page Path: /page/4
----------------------------------------
Page Number: 5
Page Path: /page/5
----------------------------------------
Page Number: 6
Page Path: /page/6
----------------------------------------
Page Number: 7
Page Path: /page/7
----------------------------------------
Page Number: 8
Page Path: /page/8
----------------------------------------
Page Number: 9
Page Path: /page/9
----------------------------------------
Source Code
Use the following link to get the source code:
Example | Source Code Link |
---|---|
Example #1: Pagination Iterator | GitHub |
Other Code Implementations
Use the following links to check the Iterator pattern implementation in other programming languages.