<p>The <strong>iterator pattern</strong> comes under the <strong><a href="https://dineshonjava.com/behavioral-design-pattern/">behavioral patterns</a></strong>. The design uses iterator object, which stores the track of the current object in the list and the next object which is supposed to be iterated next. Iterator pattern takes the responsibility for the accessing and passing of the objects through the list and storing them in an iterator object.</p>
<h2>Iterator Pattern</h2>
<div style="text-align: left;">
<blockquote><p><strong>According to the Gang of Four:</strong></p>
<div style="text-align: left;">Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.</div>
</blockquote>
</div>
<p>Iterator, in the sense of computer programming, is an object that allows the programmer to traverse a container, specifically lists. The interface of the container is the source for different types of iterators. Iterators are often tightly linked to the container to allow the operational semantics of the iterator.</p>
<div style="text-align: left;">
<blockquote><p><strong>also read:</strong></p>
<ul style="text-align: left;">
<li><a title="https://dineshonjava.com/observer-pattern-design-patterns-java/" href="https://dineshonjava.com/observer-pattern-design-patterns-java/">Observer Design Pattern</a></li>
<li><a title="https://dineshonjava.com/chain-responsibility-pattern/" href="https://dineshonjava.com/chain-responsibility-pattern/">Chain of responsibility Pattern</a></li>
<li><a title="https://dineshonjava.com/command-pattern-design-patterns-java/" href="https://dineshonjava.com/command-pattern-design-patterns-java/">Command Design Pattern</a></li>
<li><a title="https://dineshonjava.com/strategy-pattern-design-patterns-java/" href="https://dineshonjava.com/strategy-pattern-design-patterns-java/">Strategy Design Pattern</a></li>
<li><a title="https://dineshonjava.com/template-method-pattern-design-java/" href="https://dineshonjava.com/template-method-pattern-design-java/">Template method Design Pattern</a></li>
<li><a title="https://dineshonjava.com/interpreter-pattern-design-patterns-java/" href="https://dineshonjava.com/interpreter-pattern-design-patterns-java/">Interpreter Design Pattern</a></li>
<li><a title="https://dineshonjava.com/mediator-pattern-design-patterns-java/" href="https://dineshonjava.com/mediator-pattern-design-patterns-java/">Mediator Design Pattern</a></li>
<li><a title="https://dineshonjava.com/memento-pattern-design-patterns-java/" href="https://dineshonjava.com/memento-pattern-design-patterns-java/">Memento Design Pattern</a></li>
<li><a title="https://dineshonjava.com/creational-design-patterns/" href="https://dineshonjava.com/creational-design-patterns//">Creational GoF Design Patterns</a></li>
<li><a title="https://dineshonjava.com/structural-design-patterns/" href="https://dineshonjava.com/structural-design-patterns/">Structural GoF Design Patterns</a></li>
</ul>
</blockquote>
</div>
<p>To design, versatile and reusable object-oriented software, there are <a href="https://dineshonjava.com/design-patterns_25/">23 well-known design patterns</a>, the iterator pattern is one of them. The idea of Iterator pattern is to give a way to access the elements of the comprehensive object in a sequential manner without having the need to know about its hidden representation. For example, if a person writes a simple binary search algorithm in Java language. Due to the strong language used, the function signature is most likely to couple the algorithm to the particular collection types. To decouple the collection types from the algorithm, the iterator interface is required. This allows the algorithm to cover various types of data structures. This way, the user does not need to write separate algorithms for each data type and can make the algorithm functions for more than one data type, just by recycling it.</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px 16px 16px 16px;">
<h2 dir="ltr">Spring 5 Design Pattern Book</h2>
<div dir="ltr" style="color: #20124d; font-family: 'calibri' , sans-serif; font-size: 13pt; text-align: justify;">You could purchase my <strong>Spring 5 book</strong> that is with title name &#8220;<strong>Spring 5 Design Patterns</strong>&#8220;. This book is available on the <a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?ie=UTF8&;qid=1507925340&;sr=8-1&;keywords=spring+5+design+pattern" target="_blank" rel="noopener"><strong>Amazon</strong></a> and <a href="https://www.packtpub.com/application-development/spring-5-design-patterns" target="_blank" rel="noopener"><strong>Packt</strong></a> publisher website. Learn various <strong>design patterns</strong> and <strong>best practices</strong> in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- &#8220;<strong>AUTHDIS40</strong>&#8220;.</div>
<div dir="ltr"></div>
<div dir="ltr"><a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?s=books&;ie=UTF8&;qid=1512607966&;sr=1-1&;keywords=dinesh+rajput" target="_blank" rel="noopener"><img class="aligncenter wp-image-3053 size-medium" title="Spring-5-Design-Pattern" src="https://dineshonjava.com/wp-content/uploads/2013/03/Spring-5-design-pattern-243x300.jpg" width="243" height="300" /></a></div>
</div>
<h2>Benefits of using the Iterator pattern</h2>
<p>There are following benefits of the Iterator pattern:</p>
<ul>
<li>Easily access the items of the collection.</li>
<li>You can use multiple to access the item from the collection, because it support lot of variations in the traversal.</li>
<li>It provides a uniform interface for traversing different structures in a collection.</li>
</ul>
<p>The iterator pattern is used in the object-oriented programming. It is the kind of design pattern where an iterator is used to traverse through a container and its elements, this helps in decoupling the algorithm and the elements of the container. Though, in some cases, it is not possible to decouple the algorithms because they are specified by the container (depends directly on it). The pattern prevents from the representation (data structures) of the container, to be exposed. The procedure can easily be performed with the representation, hidden. For each new data type, there will be no need for new algorithms to be defined. Defining and writing down new algorithms for each different data type can consume time and energy both. The iterator pattern helps save time and energy.</p>
<p>Characterizing entry and traversal operations in the comprehensive interface may be unbendable in order that it commits the collection to specific access and traversal operations. This makes it almost impossible to add new operations in future, not unless the collection interface has been changed in respective order.</p>
<p>The iterator pattern helps to define a separate object, iterator, that takes responsibility to access and traverse the collection object. This way, other objects do not get jumbled up. The clients that use an iterator, in order to access or traverse the container or elements of the container, will not know about its underlying data structure or representation. Various types of iterators can be used to access and traverse in different ways. By declaring new iterators, new traversal operations can be defined.</p>
<h2>UML Class Structure</h2>
<p>Let&#8217;s see the following UML diagram is showing the all components of Iterator design pattern:</p>
<p><img class="aligncenter wp-image-3298 size-full" src="https://dineshonjava.com/wp-content/uploads/2017/12/iterator.gif" alt="Iterator Pattern UML diagram" width="381" height="283" /></p>
<p><strong>Iterator</strong><br />
It is an interface or abstarct class for accessing and traversing items of the collections.<br />
<strong>ConcreteIterator</strong><br />
It is implementation of the Iterator interface.<br />
<strong>Aggregate</strong><br />
It is an interface to create an Iterator object<br />
<strong>ConcreteAggregate</strong><br />
It is implementation of the Aggregate interface, it implements the Iterator creation interface to return an instance of the proper ConcreteIterator.</p>
<h2>Example of Iterator Design Pattern</h2>
<p>java.util.Iterator interface uses Iterator Design Pattern.</p>
<p><strong>Step 1: Create interfaces.</strong></p>
<p><strong>Iterator.java</strong></p>
<pre class="highlight">public interface Iterator { 
 public boolean hasNext(); 
 public Object next(); 
} 
</pre>
<p><strong>Step 2: Container.java</strong></p>
<pre class="highlight">public interface Container { 
 public Iterator getIterator(); 
} 
</pre>
<p><strong>Step 3: Create concrete class implementing the Container interface. </strong></p>
<p>This class has an inner class <em><strong>NameCollectionIterator</strong></em> implementing the Iterator interface.</p>
<p><strong>NameCollection.java</strong></p>
<pre class="highlight">public class NameRepository implements Container { 
 public String names[] = {"Dinesh", "Anamika","Arnav", "Adesh", "Vinesh"}; 
 
 @Override 
 public Iterator getIterator() { 
 return new NameCollectionIterator(); 
 } 
 
 private class NameCollectionIterator implements Iterator { 
 
 int index; 
 
 @Override 
 public boolean hasNext() { 
 
 if(index <; names.length){ 
 return true; 
 } 
 return false; 
 } 
 
 @Override 
 public Object next() { 
 
 if(this.hasNext()){ 
 return names[index++]; 
 } 
 return null; 
 }		 
 } 
} 
</pre>
<p><strong>Step 4: Create a IteratorPatternDemo class.</strong></p>
<p><strong>IteratorPatternDemo.java</strong></p>
<pre class="highlight">public class IteratorPatternDemo { 
	 
 public static void main(String[] args) { 
 NameRepository namesRepository = new NameRepository(); 
 
 for(Iterator iter = namesRepository.getIterator(); iter.hasNext();){ 
 String name = (String)iter.next(); 
 System.out.println("Name : " + name); 
 } 	 
 } 
} 
</pre>
<p><strong>Step 4: Let&#8217;s run this demo class and verify the output.</strong></p>
<pre class="highlight">Name : Dinesh 
Name : Anamika 
Name : Arnav 
Name : Adesh 
Name : Vinesh 
</pre>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/design-patterns_25/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/mediator-pattern-design-patterns-java/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '3297'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…