Iterator in Java

Last Updated : 1 Oct, 2026

An Iterator in Java is a cursor used to traverse elements of a collection one at a time. It is part of the java.util package and is commonly used with collections such as List, Set, and Queue.

  • Traverses collection elements in the forward direction.
  • Supports safe removal of elements during traversal through the optional remove() operation.

Declaration of Iterator

public interface Iterator

Here, E represents the type of elements to be iterated over.

Object Creation of Iterator

An Iterator object is created by calling the iterator() method on a collection object. Here, we will use an Iterator to traverse and print each element in an ArrayList.

Collection names = new ArrayList<>();
Iterator itr = names.iterator();

Java
import java.util.ArrayList;
import java.util.Iterator;

public class Geeks {
    public static void main(String[] args) {
      
        // Create an ArrayList and add some elements
        ArrayList<String> al = new ArrayList<>();
        al.add("A");
        al.add("B");
        al.add("C");

        // Obtain an iterator for the ArrayList
        Iterator<String> it = al.iterator();

        // Iterate through the elements and print each one
        while (it.hasNext()) {
          
            // Get the next element
            String n = it.next(); 
            System.out.println(n);      
        }
    }
}

Output
A
B
C

Hierarchy of Iterator

  • Iterator is part of the java.util package and is used by collection classes to traverse elements.
  • Collection classes implement the Iterable interface, which provides the iterator() method that returns an Iterator object.
listiterator_extends_iterator_
Iterator-cursor

Methods of Iterator Interface

The Iterator interface provides methods for traversing elements and performing operations during iteration.

  • hasNext(): Returns true if the iteration has more elements.
  • next(): Returns the next element. It throws NoSuchElementException if no element remains.
  • remove(): Removes the last element returned by next(). This is an optional operation and may not be supported by every iterator.
  • forEachRemaining(): Performs the specified action on each remaining element until all elements have been processed or an exception occurs.

Note: The remove() method may throw:

  1. UnsupportedOperationException: If the iterator does not support element removal.
  2. IllegalStateException: If remove() is called before next(), or if it is called more than once after the same call to next().

Internal Working of Iterator

In this section, we will try to understand how Java Iterator and its methods work internally. Let us take the following LinkedList object to understand this functionality.

Step 1: Now, let us create an Iterator object on the List object as shown below:

Iterator citiesIterator = cities.iterator();

The "citiesIterator" iterator will look like below:

Java Iterator Step 1
Step-1

Here Iterator’s Cursor is pointing before the first element of the List.

Step 2: Now, we will run the following code snippet.

citiesIterator.hasNext();
citiesIterator.next();

Java Iterator Step 2
Step-2

When we run the above code snippet, Iterator’s Cursor points to the first element in the list as shown in the above diagram.

Step 3: Now, we will run the following code snippet.

citiesIterator.hasNext();
citiesIterator.next();

Java Iterator Step 3
Step-3


When we run the above code snippet, Iterator’s Cursor points to the second element in the list as shown in the above diagram.

Step 4: Do this process to reach the Iterator’s Cursor to the end element of the List.

Java Iterator Step n
Step-4

Step 5: After reading the final element, if we run the below code snippet, it returns a “false” value.

citiesIterator.hasNext();

Java Iterator at the end

As Iterator’s Cursor points to the after the final element of the List, hasNext() method returns a false value.

Note: Java Iterator supports only forward direction iteration, so it is known as a uni-directional cursor. In contrast, ListIterator is bi-directional, allowing traversal in both directions.

Java-Ilterator-6
Java Iterator: Forward Direction

Example: Here, we will use an Iterator to traverse and remove odd elements from an ArrayList.

Java
import java.util.ArrayList;
import java.util.Iterator;

public class Geeks {

    public static void main(String[] args) {
        
        // Creating an ArrayList of Integer type
        ArrayList<Integer> al = new ArrayList<>();

        // Adding elements to the ArrayList
        for (int i = 0; i < 10; i++) {
            al.add(i);
        }

        // Printing the original list
        System.out.println("Original List: " + al);

        // Creating an Iterator for the ArrayList
        Iterator<Integer> itr = al.iterator();

        // Iterating through the list and removing odd elements
        while (itr.hasNext()) {
          
            // Getting the next element
            int i = itr.next();  
            
            System.out.print(i + " ");  

            // Removing odd elements
            if (i % 2 != 0) {
                itr.remove();
            }
        }

        System.out.println();

        // Printing the modified list after removal of odd elements
        System.out.println("Modified List: " + al);
    }
}

Output
Original List: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0 1 2 3 4 5 6 7 8 9 
Modified List: [0, 2, 4, 6, 8]

Explanation: In the above example, we create an ArrayList of integers then iterates through it using an Iterator and removes all odd numbers. It prints the list before and after removing the odd elements and prints the modified list with only even numbers.

Comment