Last Updated:

Iterator in java

When you have a collection, you want to access its items, add/remove, or process items. To do all this processing with a Java program, you must be able to go through the collection you are using. This is where the iterator takes the stage.

What is the Java Iterator?

In Java, an iterator is a construct that is used to traverse or step through a collection.
To use an iterator, you need to get an iterator object using the "iterator()" method of the collection interface. The Java Iterator is the interface of the data collection platform and is part of the "java.util" package. Using the Java iterator, you can iterate through the collection of objects.

The Java iterator interface replaces the enumerator that was previously used to step through some simple collections, such as vectors. The main differences between a Java iterator and an enumerator are as follows:

  1. Significant improvement in method names.
  2. You can remove method items from a collection that is viewed using an iterator.

In this tutorial, we'll discuss the details of the iterator interface and the ListIterator interface, which is a bidirectional interface.

Types of iterators:

  • Enumerator
  • Iterator
  • Listerator

The enumerator is now rarely used. So we'll focus on the iterator and listerator interfaces.

Iterator interface in Java

The iterator interface in Java is part of the collection framework in the 'java.util' package and is a cursor that can be used to step through a collection of objects. The iterator interface has the following main characteristics:

  1. The iterator interface is available starting with the Java 1.2 data collection platform.
  2. It traverses a collection of objects one by one.
  3. It is popularly known as the "Universal Java Cursor" because it works with all collections.
  4. This interface supports "read" and "delete" operations, i.e. you can delete an item during iteration using an iterator.

A general representation of the iterator interface is given below:

Iterator in java

Next, let's look at the iterator methods listed above.

Iterator methods

The iterator interface supports the following methods:


Prototype: E next ()

Parameters: No parameters

Type of return: E ->element

Description: Returns the next item in the collection.

If there are no more items in the collection, it throws a NoSuchElementException.


Prototype: logical hasNext()

Options: Nil

Return type: true => there are items in the collection. False => no more elements

Description: The hasNext() function checks to see if there are more items in the collection that is accessed by an iterator. If there are no more elements, you do not call the next() method. In other words, you can use this function to decide whether to call the next() method.


Prototype: voidremove()

Options: NIL

Type of return: NIL

Description: Deletes the last item returned by an iterator that iterates through the underlying collection. The remove() method can only be called once on the next call ().

If the iterator does not support a delete operation, it throws an UnsupportedOperationException.


Prototype: void forEachRemaining(consumer action<? super E> )

Parameters: action =>the action to be performed

Return Type: void

Description: Performs the specified action on each of the remaining items in the collection until all items are exhausted or the action throws an exception. Exceptions caused by the action are passed to the caller.

If the action is null, it throws a NullPointerException. This feature is a new addition to the iterator interface in Java 8.

Java Iterator Example

Let's implement a Java program to demonstrate the use of the iterator interface. The following program creates an ArrayList of colors. It then retrieves an iterator using the iterator() ArrayList method. The list is then viewed to display each item.

Iterator in java

Interface limitations:

  1. An operation to replace an item or add a new item cannot be performed by using this iterator.
  2. The iteration continues in only one direction, that is, in the forward direction.
  3. Supports only sequential iteration.
  4. When you want to iterate through large amounts of data, it affects iterator performance.

What is ListIterator

The main features of the ListIterator interface include:

  • The ListIterator interface extends the iterator interface.
  • The ListIterator interface supports CRUD operations, i.e. create, read, update, and delete.
  • Supports iteration in both forward and reverse directions.
  • Because this interface is bidirectional, the cursor is always positioned between the previous and next elements.
  • This interface basically works for list implementations like ArrayList, LinkedList, etc.
  • Available with Java 1.2

The ListIterator interface is represented as follows:

Iterator in java

As mentioned, ListIterator extends the iterator interface. Thus, in addition to supporting all the methods of the iterator interface, as shown above, the ListIterator interface also has its own methods that help it perform CRUD operations, as well as bidirectional iteration.

Listerator methods

Note that the next(), hasNext(), and remove() iterator interface methods work in exactly the same way as the ListIterator interface. Therefore, we will skip these methods in this section. In addition to the above methods, ListIterator has the following methods:

Previous ()

Prototype: E Previous( ()

Options: NIL

Return type: E is the previous item in the list. -1 – if the iterator is at the top of the list.

Description: This function returns the previous item in the list. As soon as the previous element is returned, the cursor moves back to the next element.


Prototype: booleanhasPrevious()

Options: NIL

The return type is true => iterator has more items when the list is crawled back.

Description: This function checks whether the ListIterator has more items in the opposite direction.


Prototype: intpreviousIndex()

Options: NIL

Return type: int is the index of the previous item. -1 – if the pointer is at the top of the list.

Description: Returns the index of the previous item returned by the previous call ().


Prototype: intnextIndex()

Options: NIL

The type of the return value: int is the following index. -1 - if the iterator is at the end of the list.

Description: Returns the following index of the item in the list. This element is returned by calling the next() method.


Prototype: void set(E e)

Parameters: e is the element to be replaced

Type of return: NIL

Description: Used to replace the last element with the specified e element.


Prototype: void add(E e)

Parameters: e is the element to be added

Type of return: NIL

Description: Adds new items to the list at the position preceding the position of the next item ().


In this article, we discussed iterators in Java that are used with collections. This knowledge will help readers understand the collections we are going to explore later.