Java Iterator

Last Updated : 4 Apr 2026

Java Iterator is an important tool, which allows developers to traverse elements in a collection in a simple and consistent way.

In this chapter, you will learn about the Java Iterator, its purpose, features, and how it is used to iterate over elements in a collection.

What is Java Iterator?

An Iterator is an interface used to traverse elements of a collection one by one. It is part of the Java Collections Framework and belongs to the java.util package. The Iterator interface was introduced in Java 1.2.

Iterator provides a standard way to access elements in collections such as ArrayList, HashSet, and other collection classes. It is called a universal cursor because it can be used with all classes that implement the Collection interface.

Before Iterator, Java used the Enumeration interface to traverse elements. However, Enumeration was limited and only supported read operations. Iterator improved upon Enumeration by providing additional functionality, such as the ability to remove elements during iteration.

The Iterator interface mainly supports the following operations:

  • Read (traverse elements)
  • Remove elements safely during iteration

Compared to Enumeration, the method names in Iterator are more clear, consistent, and easier to use that make it the preferred choice for traversing collections in modern Java programming.

How to use Java Iterator?

To use a Java Iterator, first obtain an Iterator instance from a collection (such as ArrayList or HashSet). The Iterator then allows you to traverse the elements one by one.

The Iterator keeps track of the current position in the collection, ensuring each element is accessed sequentially.

If the collection is modified while iterating (except through the Iterator’s own remove() method), it will throw a ConcurrentModificationException during the next operation.

Java Iterator Methods

The Java Iterator interface provides methods to traverse and modify elements in a collection. It contains a total of four methods:

1. hasNext()

This method does not accept any parameters. It returns true if there are more elements remaining in the iteration; otherwise, it returns false.

If there are no more elements, there is no need to call the next() method. In simple terms, this method is used to determine whether next() should be called or not.

The syntax of this method is:

2. next()

This method does not accept any parameters. It returns the next element (E) in the traversal.

If there are no more elements in the iteration, it throws a NoSuchElementException.

The syntax of this method is:

3. remove()

This method does not accept any parameters and does not return any value. It removes the last element returned by the iterator from the underlying collection.

The remove() method can be called only once per call to next().

  • If the iterator does not support the remove operation, it throws an UnsupportedOperationException.
  • If next() has not been called before calling remove(), it throws an IllegalStateException.

The syntax of this method is:

4. forEachRemaining()

This method accepts a parameter action, which represents the operation to be performed on each remaining element. It does not return any value.

This method processes all remaining elements in the collection until all elements are consumed or an exception occurs.

  • If the action throws an exception, it is passed to the caller.
  • If the action is null, it throws a NullPointerException.

The forEachRemaining() method was introduced in Java 8.

The syntax of this method is:

Example of Java Iterator

In this example, we create an ArrayList of city names. Then, we initialize an Iterator using the iterator() method of the ArrayList. After that, we traverse the list to display each element one by one.

Example

Compile and Run

Output:

CityNames elements: 
Delhi Mumbai Kolkata Chandigarh Noida

Next TopicJava Deque