The “ArrayList” in Java is vital to store multiple sorts of data in one place which assists in accessing this data conveniently in the future. This data can be of various data types. To access and iterate this data, the “iterator()” method comes into effect that iterates through the elements and returns them in a streamlined way.
This blog will elaborate on the ArrayList “iterator()” method in Java.
What is ArrayList “iterator()” in Java?
The Java ArrayList “iterator()” method returns an iterator to access each element of the ArrayList in an appropriate sequence.
Syntax
arraylist.iterator()
This method does not take any parameters.
Return Value
This method returns an iterator to loop the ArrayList elements.
The two associated methods that assist in iteration are as follows:
- hasNext(): This method returns true if there is a contained next element in the ArrayList.
- next(): It gives the next element in the ArrayList.
These specific methods assist in avoiding the faced limitations while iteration.
Import the below-stated packages in all the examples of this guide to work with the “ArrayList” and “Iterator” classes, respectively:
import java.util.ArrayList;
import java.util.Iterator;
Example 1: Applying the ArrayList “iterator()” in Java
This example implements the ArrayList “iterator()” method to iterate through each ArrayList element:
public class Iteratorclass {
public static void main(String[] args){
ArrayList<String> items = new ArrayList<>();
items.add("Dhaka");
items.add("London");
items.add("Canberra");
Iterator<String> iteration = items.iterator();
System.out.print("ArrayList Items -> ");
while(iteration.hasNext()){
System.out.print(iteration.next() + " ");
}
}}
In this code snippet:
- First of all, create an” ArrayList” of the “String” type and append the stated items in it utilizing the “add()” method.
- Now, use the “Iterator” interface, associate it with the stated variable and apply the “iterator()” method that iterates through the target ArrayList.
- Lastly, implement the combined “hasNext()” and “next()” methods to iterate the ArrayList elements, if contained.
Output

The output signifies that the ArrayList items are iterated appropriately.
Example 2: Iterating the Elements and Returning their Indexes Using the ArrayList “iterator()” in Java
In this demonstration, the “iterator()” method iterates through the ArrayList elements and the “indexOf()” method logs the index of the iterated elements:
public class Iteratorclass {
public static void main(String[] args){
ArrayList<Object> items = new ArrayList<>();
items.add("Dhaka");
items.add("London");
items.add(3);
Iterator<Object> iteration = items.iterator();
System.out.println("Item: Index");
while(iteration.hasNext()){
Object element = iteration.next();
System.out.print(element + ": ");
System.out.println(items.indexOf(element));
}
}}
According to this block of code:
- Define an ArrayList of “Object” type instead that comprises both the stated string and integer values.
- Now, likewise, use the Iterator interface and utilize the “iterator()” method to iterate the ArrayList items with the help of the “hasNext()” and “next()” methods.
- Finally, implement the “indexOf()” method to return the index of the iterated ArrayList items as well.
Output

As analyzed, the elements along with their indexes are displayed accordingly.
Conclusion
The Java ArrayList “iterator()” method returns an iterator to access/iterate each element of the ArrayList in an appropriate sequence. This method can also be applied with the “hasNext()” and “next()” methods to streamline the iteration, thereby avoiding the faced limitations. This article discussed applying the ArrayList “iterator()” in Java.