Open In App

ListIterator in Java

Last Updated : 30 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

ListIterator is one of the four Java cursors. It is an iterator used to traverse all types of lists, such as ArrayList, LinkedList, Vector and Stack. It was introduced in Java 1.2 and extends the Iterator interface, providing bi-directional traversal along with support for all CRUD operations.

Hierarchy of ListIterator

listiterator_extends_iterator_
Hierarchy

Key Features of ListIterator

  • It works only with list-implemented classes.
  • It supports bi-directional traversal (forward & backward).
  • Supports Create, Read, Update, Delete (CRUD) operations.
  • Introduced in Java 1.2.

Interesting Fact about ListIterator

There is no current element in ListIterator. Its cursor always lies between the previous and next elements. The previous() will return to the previous elements and the next() will return to the next element. Therefore, for a list of length n, there are n+1 possible cursors.

position_of_cursor
Position

Syntax: Declaration

public interface ListIterator<E> extends Iterator<E>

Where E represents the generic type i.e any parameter of any type/user-defined object.

Getting a ListIterator

ListIterator<E> listIterator()

This returns the list iterator of all the elements of the list.

Example:

Java
import java.util.*;

public class ListIteratorDemo {
  
    // Main driver method 
    public static void main(String[] args)
    {
        // Creating a list of names
        List<String> names = new LinkedList<>();
      
        names.add("Welcome");
        names.add("To");
        names.add("Gfg");

        // Getting ListIterator
        ListIterator<String> namesIterator
            = names.listIterator();

        // Traversing elements using next() method 
        while (namesIterator.hasNext()) {
            System.out.println(namesIterator.next());
        }

        // for-each loop creates Internal Iterator here.
        for (String s : names) {
            System.out.println(s);
        }
    }
}

Output
Welcome
To
Gfg
Welcome
To
Gfg

Forward and Backward Traversal

1. Forward direction iteration

  • hasNext(): This method returns true when the list has more elements to traverse while traversing in the forward direction
  • next(): This method returns the next element of the list and advances the position of the cursor.
  • nextIndex(): This method returns the index of the element that would be returned on calling the next() method.

2. Backward direction iteration

  • hasPrevious(): This method returns true when the list has more elements to traverse while traversing in the reverse direction
  • previous(): This method returns the previous element of the list and shifts the cursor one position backward.
  • previousIndex(): This method returns the index of the element that would be returned on calling the previous() method.

Example

Java
import java.util.*;

public class GFG {
  
    public static void main(String[] args)
    {
          // list of names
        List<String> names = new LinkedList<>();
        names.add("learn");
        names.add("from");
        names.add("Geeksforgeeks");

        // Getting ListIterator
        ListIterator<String> listIterator
            = names.listIterator();

        // Traversing elements
        System.out.println("Forward Direction Iteration:");
        while (listIterator.hasNext()) {
            System.out.println(listIterator.next());
        }

        // Traversing elements, the iterator is at the end at this point
        System.out.println("Backward Direction Iteration:");
        while (listIterator.hasPrevious()) {
            System.out.println(listIterator.previous());
        }
    }
}

Output
Forward Direction Iteration:
learn
from
Geeksforgeeks
Backward Direction Iteration:
Geeksforgeeks
from
learn

Difference between Iterator and ListIterator

Similarities:

  • They both are introduced in java 1.2.
  • They both are used for iteration lists.
  • They both support forward direction traversal.
  • They both supports READ and DELETE operations.

                Iterator

                                           ListIterator

It can traverse a collection of any type. It traverses only list collection implemented classes like LinkedList, ArrayList, etc.
Traversal can only be done in forwarding direction.Traversal of elements can be done in both forward and backward direction.
Iterator object can be created by calling iterator() method of the collection interface.ListIterator object can be created by calling directions listIterator() method of the collection interface.
Deletion of elements is allowed using remove() method.Deletion of elements is allowed.
It throws ConcurrentModificationException on doing addition operation. Hence, addition is not allowed. Addition of elements is allowed.
In iterator, we can't access the index of the traversed element.In listIterator, we have nextIndex() and previousIndex() methods for accessing the indexes of the traversed or the next traversing element.
Modification of any element is not allowed.Modification is allowed.

Methods of ListIterator

Method

Description

add(E e)This method inserts the specified element into the list.
hasNext(),This returns true if the list has more elements to traverse.
hasPrevious()This returns true if the list iterator has more elements while traversing the list in the backward direction.
next()This method returns the next element and increases the cursor by one position.
nextIndex()This method returns the index of the element which would be returned on calling the next() method.
previous()This method returns the previous element of the list and shifts the cursor one position backward
previousIndex()This method returns the index of the element which would be returned on calling the previous() method.
remove()This method removes the last element from the list that was returned on calling next() or previous() method element from.
set(E e)This method replaces the last element that was returned on calling next() or previous() method with the specified element.

ListIterator in Java
Visit Course explore course icon
Article Tags :