An Iterator in Java is one of the most commonly used cursors in the Java Collections Framework. It is used to traverse or iterate through elements of a collection one by one.
- It is used to traverse elements in the forward direction only.
- Removes elements safely during traversal using remove().
- Iterator is a universal cursor that applies to all collection types — List, Set, and Queue.
Declaration of Iterator
public interface Iterator<E>
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<String> names = new ArrayList<>();
Iterator<String> 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);
}
}
}
Hierarchy of Iterator
Iterator is part of the java.util package and is implemented by all collection classes through their subinterfaces.
Iterator-cursorMethods of Iterator Interface
The iterator interface defines three methods as listed below:
- hasNext(): Returns true if the iteration has more elements.
- next(): Returns the next element in the iteration. It throws NoSuchElementException if no more element is present.
- remove(): Removes the last element returned by next(). This method can be called only once per call to next().
Note: remove() method can throw two exceptions namely as follows:
Internal Working
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<String> citiesIterator = cities.iterator();
The "citiesIteartor" iterator will look like below:
Step-1Here 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();
Step-2When 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();
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.
Step-4Step 5: After reading the final element, if we run the below code snippet, it returns a “false” value.
citiesIterator.hasNext();

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 Iterator: Forward DirectionExample: 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);
}
}
OutputOriginal 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.
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java