{"id":44864,"date":"2015-09-30T06:49:40","date_gmt":"2015-09-30T03:49:40","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=44864"},"modified":"2023-12-08T09:41:48","modified_gmt":"2023-12-08T07:41:48","slug":"iterator-design-pattern","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2015\/09\/iterator-design-pattern.html","title":{"rendered":"Iterator Design Pattern Example"},"content":{"rendered":"<p><em>This article is part of our Academy Course titled <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/java-design-patterns\/\">Java Design Patterns<\/a>.<\/em><\/p>\n<p>In this course you will delve into a vast number of Design Patterns and see how those are implemented and utilized in Java. You will understand the reasons why patterns are so important and learn when and how to apply each one of them. Check it out <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/java-design-patterns\/\">here<\/a>!<\/p>\n<div class=\"toc\">\n<h4>Table Of Contents<\/h4>\n<dl>\n<dt><a href=\"#introduction\">1. Introduction<\/a><\/dt>\n<dt><a href=\"#what\">2. What is the Iterator Design Pattern <\/a><\/dt>\n<dt><a href=\"#implement\">3. Implementing the Iterator Design Pattern<\/a><\/dt>\n<dt><a href=\"#intext\">4. Internal and External Iterators<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#intext_in \">4.1. Internal Iterators<\/a><\/dt>\n<dt><a href=\"#intext_ext \">4.2. External Iterators<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#when\">5. When to use the Iterator Design Pattern<\/a><\/dt>\n<dt><a href=\"#patterninjdk\">6. Iterator Pattern in JDK<\/a><\/dt>\n<dt><a href=\"#download\">7. Download the Source Code<\/a><\/dt>\n<\/dl>\n<\/div>\n<h2><a name=\"introduction\"><\/a>1. Introduction<\/h2>\n<p>An aggregate object, such as a list, should give you a way to access its elements without exposing its internal structure. Moreover, you might want to traverse the list in different ways, depending on what you want to accomplish. But you probably don&#8217;t want to bloat the <code>List<\/code> interface with operations for different traversals, even if you could anticipate the ones you will need. You might also need to have more than one traversal pending on the same list.<\/p>\n<p>The Iterator pattern lets you do all this. The key idea in this pattern is to take the responsibility for access and traversal out of the list object and put it into an iterator object. The <code>Iterator<\/code> class defines an interface for accessing the list&#8217;s elements. An iterator object is responsible for keeping track of the current element; that is, it knows which elements have been traversed already.<\/p>\n<h2><a name=\"what\"><\/a>2. What is the Iterator Design Pattern<\/h2>\n<p>The intent of the Iterator Design Pattern is to provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.<\/p>\n<p>The Iterator pattern allows a client object to access the contents of a container in a sequential manner, without having any knowledge about the internal representation of its contents. The term container, used above, can simply be defined as a collection of data or objects. The objects within the container could in turn be collections, making it a collection of collections.<\/p>\n<p>The Iterator pattern enables a client object to traverse through this collection of objects (or the container) without having the container to reveal how the data is stored internally. To accomplish this, the Iterator pattern suggests that a Container object should be designed to provide a public interface in the form of an Iterator object for different client objects to access its contents. An Iterator object contains public methods to allow a client object to navigate through the list of objects within the container.<\/p>\n<p><figure id=\"attachment_7756\" aria-describedby=\"caption-attachment-7756\" style=\"width: 438px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/iterator_design_pattern_class_diagram_1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-7756\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/iterator_design_pattern_class_diagram_1.jpg\" alt=\"Figure 1- Class diagram\" width=\"438\" height=\"137\" \/><\/a><figcaption id=\"caption-attachment-7756\" class=\"wp-caption-text\">Figure 1- Class diagram<\/figcaption><\/figure><div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p><strong>Iterator<\/strong><\/p>\n<ul>\n<li>Defines an interface for accessing and traversing elements.<\/li>\n<\/ul>\n<p><strong>ConcreteIterator<\/strong><\/p>\n<ul>\n<li>Implements the Iterator interface.<\/li>\n<li>Keeps track of the current position in the traversal of the aggregate.<\/li>\n<\/ul>\n<p><strong>Aggregate<\/strong><\/p>\n<ul>\n<li>Defines an interface for creating an Iterator object.<\/li>\n<\/ul>\n<p><strong> ConcreteAggregate<\/strong><\/p>\n<ul>\n<li>Implements the Iterator creation interface to return an instance of the proper ConcreteIterator.<\/li>\n<\/ul>\n<h2><a name=\"implement\"><\/a>3. Implementing the Iterator Design Pattern<\/h2>\n<p>Let us implement the Iterator Design Pattern using a <code>Shape<\/code> class. We will store and iterate the <code>Shape<\/code> objects using an iterator.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.iteratorpattern;\n\npublic class Shape {\n\n\tprivate int id;\n\tprivate String name;\n\t\n\tpublic Shape(int id, String name){\n\t\tthis.id = id;\n\t\tthis.name = name;\n\t}\n\t\n\tpublic int getId() {\n\t\treturn id;\n\t}\n\tpublic void setId(int id) {\n\t\tthis.id = id;\n\t}\n\tpublic String getName() {\n\t\treturn name;\n\t}\n\tpublic void setName(String name) {\n\t\tthis.name = name;\n\t}\n\t\n\t@Override\n\tpublic String toString(){\n\t\treturn \"ID: \"+id+\" Shape: \"+name;\n\t}\n\t\n}\n<\/pre>\n<p>The simple <code>Shape<\/code> class has an <code>id<\/code> and <code>name<\/code> as its attributes.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.iteratorpattern;\n\npublic class ShapeStorage {\n\t\n\tprivate Shape []shapes = new Shape[5];\n\tprivate int index;\n\t\n\tpublic void addShape(String name){\n\t\tint i = index++;\n\t\tshapes[i] = new Shape(i,name);\n\t}\n\t\n\tpublic Shape[] getShapes(){\n\t\treturn shapes;\n\t}\n}\n<\/pre>\n<p>[ulp id=&#8217;7eyRwVlMDyxg6Ptw&#8217;]<br \/>\n&nbsp;<br \/>\nThe above class is used to store the <code>Shape<\/code> objects. The class contains an array of <code>Shape<\/code> type, for simplicity we have initialized that array up to 5. The <code>addShape<\/code> method is used to add a <code>Shape<\/code> object to the array and increment the index by one. The <code>getShapes<\/code> method returns the array of <code>Shape<\/code> type.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.iteratorpattern;\n\nimport java.util.Iterator;\n\npublic class ShapeIterator implements Iterator&lt;Shape&gt;{\n\n\tprivate Shape [] shapes;\n\tint pos;\n\t\n\tpublic ShapeIterator(Shape []shapes){\n\t\tthis.shapes = shapes;\n\t}\n\t@Override\n\tpublic boolean hasNext() {\n\t\tif(pos &gt;= shapes.length || shapes[pos] == null)\n\t\t\treturn false;\n\t\treturn true;\n\t}\n\n\t@Override\n\tpublic Shape next() {\n\t\treturn shapes[pos++];\n\t}\n\n\t@Override\n\tpublic void remove() {\n\t\tif(pos &lt;=0 )\n\t\t\tthrow new IllegalStateException(\"Illegal position\");\n\t\tif(shapes[pos-1] !=null){\n\t\t\tfor (int i= pos-1; i&lt;(shapes.length-1);i++){\n\t\t\t\tshapes[i] = shapes[i+1];\n\t\t\t}\n\t\t\tshapes[shapes.length-1] = null;\n\t\t}\n\t}\n}\n<\/pre>\n<p>The above class is an <code>Iterator<\/code> to the <code>Shape<\/code> class. The class implements the <code>Iterator<\/code> interface and defines all the methods of the <code>Iterator<\/code> interface.<\/p>\n<p>The <code>hasNext<\/code> method returns a <code>boolean<\/code> if there\u2019s an item left. The next method returns the next item from the collection and the remove method remove the current item from the collection.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.iteratorpattern;\n\npublic class TestIteratorPattern {\n\n\tpublic static void main(String[] args) {\n\t\tShapeStorage storage = new ShapeStorage();\n \t\tstorage.addShape(\"Polygon\");\n\t\tstorage.addShape(\"Hexagon\");\n\t\tstorage.addShape(\"Circle\");\n\t\tstorage.addShape(\"Rectangle\");\n\t\tstorage.addShape(\"Square\");\n\t\t\n\t\tShapeIterator iterator = new ShapeIterator(storage.getShapes());\n\t\twhile(iterator.hasNext()){\n\t\t\tSystem.out.println(iterator.next());\n\t\t}\n\t\tSystem.out.println(\"Apply removing while iterating...\");\n\t\titerator = new ShapeIterator(storage.getShapes());\n\t\twhile(iterator.hasNext()){\n\t\t\tSystem.out.println(iterator.next());\n\t\t\titerator.remove();\n\t\t}\n\t}\n\n}\n<\/pre>\n<p>The above code will result to the following output:<\/p>\n<pre class=\"brush:bash\">ID: 0 Shape: Polygon\nID: 1 Shape: Hexagon\nID: 2 Shape: Circle\nID: 3 Shape: Rectangle\nID: 4 Shape: Square\nApply removing while iterating...\nID: 0 Shape: Polygon\nID: 2 Shape: Circle\nID: 4 Shape: Square\n<\/pre>\n<p>In the above class, we have created a <code>ShapeStorage<\/code> object and stores the <code>Shape<\/code> objects in it. Next, we created a <code>ShapeIterator<\/code> object and assigned it the shapes. We iterated twice, first without calling the remove method and then with the remove method.<\/p>\n<p>The output shows you the impact of the remove method. At first iteration, the iterator prints all the shapes but when the remove method is called, it first prints the current shape and moved to the next shape. Then, we called the remove method on it which removes the current shape and then the iterator points to the next available shape object.<\/p>\n<p>That\u2019s why, the output after \u201cApply removing while iterating\u2026\u201d shows only 0, 2, and 4 shapes object.<\/p>\n<h2><a name=\"intext\"><\/a>4. Internal and External Iterators<\/h2>\n<p>An iterator can be designed either as an internal iterator or as an external iterator.<\/p>\n<h3><a name=\"intext_in\"><\/a>4.1 Internal Iterators<\/h3>\n<ul>\n<li>The collection itself offers methods to allow a client to visit different objects within the collection. For example, the <code>java.util.ResultSet<\/code> class contains the data and also offers methods such as next to navigate through the item list.<\/li>\n<li>There can be only one iterator on a collection at any given time.<\/li>\n<li>The collection has to maintain or save the state of iteration.<\/li>\n<\/ul>\n<h3><a name=\"intext_ext\"><\/a>4.2 External Iterators<\/h3>\n<ul>\n<li>The iteration functionality is separated from the collection and kept inside a different object referred to as an iterator. Usually, the collection itself returns an appropriate iterator object to the client depending on the client input. For example, the <code>java.util.Vector<\/code> class has its iterator defined in the form of a separate object of type <code>Enumeration<\/code>. This object is returned to a client object in response to the <code>elements()<\/code> method call.<\/li>\n<li>There can be multiple iterators on a given collection at any given time.<\/li>\n<li>The overhead involved in storing the state of iteration is not associated with the collection. It lies with the exclusive Iterator object.<\/li>\n<\/ul>\n<h2><a name=\"when\"><\/a>5. When to use the Iterator Design Pattern<\/h2>\n<p>Use the Iterator pattern:<\/p>\n<ul>\n<li>To access an aggregate object&#8217;s contents without exposing its internal representation.<\/li>\n<li>To support multiple traversals of aggregate objects.<\/li>\n<li>To provide a uniform interface for traversing different aggregate structures (that is, to support polymorphic iteration).<\/li>\n<\/ul>\n<h2><a name=\"patterninjdk\"><\/a>6. Iterator Pattern in JDK<\/h2>\n<ul>\n<li><code>java.util.Iterator<\/code><\/li>\n<li><code>java.util.Enumeration<\/code><\/li>\n<\/ul>\n<h2><a name=\"download\"><\/a>7. Download the Source Code<\/h2>\n<p>This was a lesson on the Iterator Design Pattern. You may download the source code here: <a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/IteratorPattern-Project.zip\"><strong>IteratorPattern-Project<\/strong><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how those are implemented and utilized in Java. You will understand the reasons why patterns are so important and learn when and how to apply each one of &hellip;<\/p>\n","protected":false},"author":967,"featured_media":148,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[145],"class_list":["post-44864","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java","tag-design-patterns"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Iterator Design Pattern Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2015\/09\/iterator-design-pattern.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Iterator Design Pattern Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2015\/09\/iterator-design-pattern.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2015-09-30T03:49:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-08T07:41:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Rohit Joshi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rohit Joshi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/iterator-design-pattern.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/iterator-design-pattern.html\"},\"author\":{\"name\":\"Rohit Joshi\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/ddd1b95af381c5042c09833e6a108e20\"},\"headline\":\"Iterator Design Pattern Example\",\"datePublished\":\"2015-09-30T03:49:40+00:00\",\"dateModified\":\"2023-12-08T07:41:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/iterator-design-pattern.html\"},\"wordCount\":1009,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/iterator-design-pattern.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"keywords\":[\"Design Patterns\"],\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/iterator-design-pattern.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/iterator-design-pattern.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/iterator-design-pattern.html\",\"name\":\"Iterator Design Pattern Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/iterator-design-pattern.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/iterator-design-pattern.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"datePublished\":\"2015-09-30T03:49:40+00:00\",\"dateModified\":\"2023-12-08T07:41:48+00:00\",\"description\":\"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/iterator-design-pattern.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/iterator-design-pattern.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/iterator-design-pattern.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/iterator-design-pattern.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/core-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Iterator Design Pattern Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/ddd1b95af381c5042c09833e6a108e20\",\"name\":\"Rohit Joshi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g\",\"caption\":\"Rohit Joshi\"},\"description\":\"Rohit Joshi is a Senior Software Engineer from India. He is a Sun Certified Java Programmer and has worked on projects related to different domains. His expertise is in Core Java and J2EE technologies, but he also has good experience with front-end technologies like Javascript, JQuery, HTML5, and JQWidgets.\",\"sameAs\":[\"http:\\\/\\\/www.javacodegeeks.com\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/rohit-joshi\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Iterator Design Pattern Example - Java Code Geeks","description":"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2015\/09\/iterator-design-pattern.html","og_locale":"en_US","og_type":"article","og_title":"Iterator Design Pattern Example - Java Code Geeks","og_description":"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how","og_url":"https:\/\/www.javacodegeeks.com\/2015\/09\/iterator-design-pattern.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2015-09-30T03:49:40+00:00","article_modified_time":"2023-12-08T07:41:48+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","type":"image\/jpeg"}],"author":"Rohit Joshi","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Rohit Joshi","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/iterator-design-pattern.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/iterator-design-pattern.html"},"author":{"name":"Rohit Joshi","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/ddd1b95af381c5042c09833e6a108e20"},"headline":"Iterator Design Pattern Example","datePublished":"2015-09-30T03:49:40+00:00","dateModified":"2023-12-08T07:41:48+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/iterator-design-pattern.html"},"wordCount":1009,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/iterator-design-pattern.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","keywords":["Design Patterns"],"articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2015\/09\/iterator-design-pattern.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/iterator-design-pattern.html","url":"https:\/\/www.javacodegeeks.com\/2015\/09\/iterator-design-pattern.html","name":"Iterator Design Pattern Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/iterator-design-pattern.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/iterator-design-pattern.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","datePublished":"2015-09-30T03:49:40+00:00","dateModified":"2023-12-08T07:41:48+00:00","description":"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/iterator-design-pattern.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2015\/09\/iterator-design-pattern.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/iterator-design-pattern.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/iterator-design-pattern.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/core-java"},{"@type":"ListItem","position":4,"name":"Iterator Design Pattern Example"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/ddd1b95af381c5042c09833e6a108e20","name":"Rohit Joshi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g","caption":"Rohit Joshi"},"description":"Rohit Joshi is a Senior Software Engineer from India. He is a Sun Certified Java Programmer and has worked on projects related to different domains. His expertise is in Core Java and J2EE technologies, but he also has good experience with front-end technologies like Javascript, JQuery, HTML5, and JQWidgets.","sameAs":["http:\/\/www.javacodegeeks.com\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/rohit-joshi"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/44864","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/967"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=44864"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/44864\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=44864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=44864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=44864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}