This post will discuss the difference between an Iterator and ListIterator interface in Java, which is used for traversing over elements of a Collection.

1. Overview of the Iterator interface

The Iterator interface is a fundamental part of the Java Collections Framework and available for all Collection implementations, such as List, Set, Queue, Deque, and also in all implemented classes of Map interface. It allows us to access elements in a collection sequentially without knowing its underlying structure. It offers three primary methods: hasNext(), next(), and remove(), which can be used to traverse collection only in the forward direction.

 
Using the hasNext() and the next() methods, we can check if there are more elements and move to those elements. However, the remove() method removes from the collection the last element returned by the next() method. Further, as we can see, the remove() method is a default method, so it’s optional. Its implementation depends on the underlying collection. For example:

Download  Run Code

2. Overview of the ListIterator interface

The ListIterator interface is a subtype of Iterator. Therefore, all the features Iterator offers are also available in ListIterator. As its name implies, ListIterator is used explicitly with lists. Apart from the three methods from the Iterator interface (hasNext(), next(), and remove()), the ListIterator interface has a set of new methods to traverse the list in both forward and backward directions.

 
Let’s take a closer look at these new methods:

1. previous() and previousIndex() method

The previous() method returns the previous element in the list and moves the cursor position backward. This method can be used to traverse the list in backward direction. The previousIndex() method returns the index of the element that would be returned by a subsequent call to previous(). For example:

Download  Run Code

2. nextIndex() method

The nextIndex() method returns the index of the element that would be returned by a subsequent call to next(). For example:

Download  Run Code

3. hasPrevious() method

The hasPrevious() method returns true if there are more elements to traverse in backward direction. For example:

Download  Run Code

4. set() method

The set() method replaces the last element returned by next() or previous() with the specified element. For example:

Download  Run Code

5. The add() method

The add() method inserts the specified element into the list at the current position of the cursor. The element is inserted before the element that would be returned by next(), if any, and after the element that would be returned by previous(), if any. The cursor is moved forward after the insertion. For example:

Download  Run Code

3. Differences between Iterator and ListIterator

Iterator and ListIterator are both useful interfaces for iterating over lists in Java, but they have some differences and trade-offs that we must understand:

  1. The major difference between an Iterator and ListIterator lies in the way they traverse the Collection. With Iterator, we can only move in forward direction using its hasNext() and next() method while with ListIterator, we can traverse the list in either direction. ListIterator provides hasNext() and next() method to travel in forward direction and hasPrevious() and previous() method to travel in backward direction.
  2. The ListIterator allows us to modify the list during iteration, using its set() method. This is not feasible with an Iterator.
  3. While traversing a list using ListIterator, we can add a new element at any point of time using its add() method. This method is not available in the Iterator class.
  4. Using Iterator, we cannot obtain the iterator’s current position in the list. On the other hand, a ListIterator can return the index of next or previous elements while traversing a list using its nextIndex() and previousIndex(), respectively.
  5. An Iterator can be used with any collection, whereas ListIterator can only be used with lists.

4. What to use and when?

As we have seen, Iterator and ListIterator are both useful interfaces for iterating over lists in Java. Here are some general recommendations on how to choose between them:

  • If you need to traverse a list in forward direction and perform read and remove operations, we should use Iterator.
  • If you need to traverse a list in both forward and backward directions and perform read, remove, add, and replace operations, we should use ListIterator.

That’s all about the differences between an Iterator and a ListIterator in Java.