# Iterator

In 
Published 2022-12-03

This tutorial explains to you the design pattern named Iterator (which is a Structural Pattern).

# Iterator Pattern - theory

Iterator Design Pattern in Java is used when we want to provide a way to access a collection of objects sequentially without exposing its underling implementation. Iteration pattern is also known as Cursor. Take a look at the following UML diagram representing the Iterator design pattern (for my example):

In this example you can see that you can traverse the list of objects in both senses. The iteration of the collection is done inside the collection class.

# Iterator Pattern - example

Here is an example of using the Iterator Design Pattern in Java:

package iterator.java.pattern.example;
 
public interface Iterator {
 
    public boolean hasNext();  
    public Object next();
    public Object current();
    public Object previous();
 
}
package iterator.java.pattern.example;
 
public interface IteratorContainer {
 
    public Iterator getIterator(); 
}
package iterator.java.pattern.example;
 
public class CollectionOfObjects implements IteratorContainer {  
    public String name[]={"Name #1", "Name #2","Name #3","Name #4"};   
     
    @Override  
    public Iterator getIterator() {  
        return new CollectionofNamesIterate() ;  
    }  
     
    private class CollectionofNamesIterate implements Iterator{  
        private int i = 0;  
         
        @Override  
        public boolean hasNext() {
            if (i < name.length ){
                  return true;
            }
            return false;
        }
         
        @Override  
        public Object previous() {  
            if(i > 1){  
                i = i-1;
                return name[i];
                
            } else { 
               i = 0;   
               return null;      
            }
        }  
         
        @Override  
        public Object current() {  
           return name[i];  
             
        } 
 
        @Override  
        public Object next() {  
            if(this.hasNext()){  
               i = i + 1;
               return name[i];  
            } else { 
               return null;      
            }
        } 
         
    }  
}
package iterator.java.pattern.example;
 
public class IteratorJavaPatternExample {
 
     public static void main(String[] args) { 
          
         String name = null;
         CollectionOfObjects cmpnyRepository = new CollectionOfObjects();  
            
         Iterator iter = cmpnyRepository.getIterator();
          
         System.out.println("Name /CURRENT/: " + (String)iter.current()); 
 
         System.out.println("Name : /NEXT/:" + (String)iter.next());    
         System.out.println("Name : /NEXT/:" + (String)iter.next()); 
          
         System.out.println("Name :/CURRENT/: " + (String)iter.current()); 
          
         System.out.println("Name : /PREVIOUS/:" + (String)iter.previous());
   }
}

When you run this example you will receive the following result: