• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to Iterate through an ArrayList using an Iterator in Java

August 31, 2023 //  by Talha Malik

The ArrayList in Java is available in java.util package. It is a resizable array that grows as the number of elements increases and acquires its original size as the elements are withdrawn. There are different methods to iterate over an ArrayList in Java like for loop, while loop, using Lambda expressions, and through iterator. 

This write-up will discuss how an iterator is used to iterate over an ArrayList in Java.

How to Iterate through an ArrayList using an Iterator in Java

The basic method of iteration over the  ArrayList in Java is to use an iterator. The main advantage of using the ArrayList is that the elements in the ArrayList can be altered and removed whereas the built-in list doesn’t allow it. Therefore, it plays an essential role when the need to modify the array arises repeatedly. One important thing to note is that the iterator() method in Java does not have any parameter declared with it.

Syntax

The syntax for the iterator method is:

arrayList.iterator()

Example1: Using Iterator Method 

Let’s go through the following coding example to learn how to iterate through an ArrayList in Java using the Iterator method:

//Import the packagesimport java.util.*;
class arraylist {
  public static void main(String[] args){
    ArrayList<String> cities = new ArrayList<>();//The add() method is responsible for adding the fields.
    cities.add("Karachi");
    cities.add("Lahore");
    cities.add("Islamabad");//Iterate over the entered cities in the array
    Iterator<String> itr = cities.iterator();
    System.out.print("ArrayList: ");
    while(itr.hasNext()){
      System.out.print(itr.next());//Print the separated output
      System.out.print(", ");
    }
  }
}

In the above code:

  • The iterator is imported from java.util package.
  • An ArrayList containing cities has been declared.
  • The add() method helps to insert a specific element in an array.
  • A variable as itr has been declared that stores the iterator and returns it to the iterator() method.
  • A value of true is returned by the hasNext method if there is a next element available in the ArrayList.
  • next() method is responsible for returning the subsequent value.

Output

The output of the code that contains all the elements in an ArrayList is declared below:

Example 2: Index of Every Element in the ArrayList Using Iterator Method()

The following code explains how to access index of every element in the ArrayList Using the Iterator Method():

import java.util.*;
class arraylist {
  public static void main(String[] args){//The values have been added to the list
    ArrayList<String> cities = new ArrayList<>();
    cities.add("Karachi");
    cities.add("Lahore");
    cities.add("Islamabad");
    Iterator<String> itr = cities.iterator();
    while(itr.hasNext()){
      String elements = itr.next();
      System.out.print(elements + ": ");//indexOf() method to access each element in the array.
      System.out.println(cities.indexOf(elements));
    }
  }
}

The above-described code is the indexOf() method that has been used to access the index of each element present in the ArrayList.

Output

The output below points to the index of each element present in the declared ArrayList of cities:

This sums up the implementation of iterating through an ArrayList using an iterator.

Conclusion

To Iterate through an ArrayList using an Iterator in Java, use the arrayList.iterator() method or the traditional looping methods. Using these methods iteration becomes straightforward and easy to implement. In this blog, we have discussed the iterator that iterates the Arraylist in detail with effective methods. 

Category: Java

Previous Post: « How to Use Java BufferedInputStream with Examples
Next Post: How to Measure Execution Time with System.currentTimeMillis() in Java »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Use Optional.ofNullable() Method in Java

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact