{"id":4145,"date":"2017-09-19T04:50:36","date_gmt":"2017-09-19T04:50:36","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=4145"},"modified":"2021-03-21T06:46:04","modified_gmt":"2021-03-21T06:46:04","slug":"how-to-traverse-a-collection-in-java","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-traverse-a-collection-in-java\/","title":{"rendered":"How to traverse a Collection in Java?"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>We can traverse a collection of objects in Java by:<\/p>\n<ul>\n<li>using Iterators.<\/li>\n<li>using the for-each construct.<\/li>\n<\/ul>\n<h3>Iterator<\/h3>\n<p>public interface Iterator&lt;E&gt;<\/p>\n<p>Iterator is an interface in the Java Collection framework that is used to iterate over a collection. An Iterator object enables us to traverse through a collection. We can get an Iterator for a collection by calling the iterator method.<\/p>\n<h3>Code listing<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.util.ArrayList;\r\nimport java.util.Iterator;\r\nimport java.util.List;\r\n\r\npublic class Demo {\r\n  List&lt;String&gt; list = new ArrayList&lt;String&gt;();\r\n  \r\n  public Demo() {\r\n    list.add(\"One\");\r\n    list.add(\"Two\");\r\n    list.add(\"Three\");\r\n    list.add(\"Four\");\r\n    list.add(\"Five\");\r\n  }\r\n  \r\n  \/\/traverse method using Iterator\r\n  public void traverse() {\r\n    Iterator&lt;String&gt; itr= list.iterator();\r\n    while(itr.hasNext()) {\r\n      System.out.println(itr.next());\r\n    }\r\n  }\r\n  \r\n  public static void main(String[] args) {\r\n    Demo d = new Demo();\r\n    d.traverse();\r\n  }\r\n}\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4159\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Iterator-collection.png\" alt=\"Iterator collection\" width=\"1554\" height=\"991\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Iterator-collection.png 1554w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Iterator-collection-300x191.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Iterator-collection-1024x653.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Iterator-collection-768x490.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Iterator-collection-1536x980.png 1536w\" sizes=\"auto, (max-width: 1554px) 100vw, 1554px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The hasNext() method returns true if the collection has more elements, and the next() method returns the next element in the collection. i.e list in this case.<\/p>\n<h3>for-each loop<\/h3>\n<p>The for-each loop allows us to traverse a collection using an enhanced for loop. For Example, the following snippet uses the for-each loop to print out each element of a collection.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.util.ArrayList;\r\nimport java.util.Iterator;\r\nimport java.util.List;\r\n\r\npublic class Demo {\r\n  List&lt;String&gt; list = new ArrayList&lt;String&gt;();\r\n  \r\n  public Demo() {\r\n    list.add(\"One\");\r\n    list.add(\"Two\");\r\n    list.add(\"Three\");\r\n    list.add(\"Four\");\r\n    list.add(\"Five\");\r\n  }\r\n  \r\n  \/\/traverse method using for each\r\n  public void traverse() {\r\n    for(String str : list) {\r\n      System.out.println(str);\r\n    }\r\n  }\r\n  \r\n  public static void main(String[] args) {\r\n    Demo d = new Demo();\r\n    d.traverse();\r\n  }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-4145\" data-postid=\"4145\" class=\"themify_builder_content themify_builder_content-4145 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Introduction We can traverse a collection of objects in Java by: using Iterators. using the for-each construct. Iterator public interface Iterator&lt;E&gt; Iterator is an interface in the Java Collection framework that is used to iterate over a collection. An Iterator object enables us to traverse through a collection. We can get an Iterator for a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-4145","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=4145"}],"version-history":[{"count":3,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4145\/revisions"}],"predecessor-version":[{"id":19743,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4145\/revisions\/19743"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=4145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=4145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=4145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}