What is the difference between Iterator and ListIterator?

Iterator and ListIterator are interfaces used to traverse collection elements, but ListIterator provides additional capabilities specifically for List implementations. While Iterator supports only forward traversal, ListIterator allows movement in both directions and supports element modification during iteration.

Key Points: • Iterator works with most collection types, whereas ListIterator works only with List implementations. • Iterator supports forward traversal only. • ListIterator supports both forward and backward traversal. • ListIterator allows adding, updating, and removing elements during iteration. • ListIterator provides methods to access element indexes.

Main Differences:

1. Collection Support

Iterator: • Can be used with List, Set, Queue, and other collection types.

ListIterator: • Can be used only with List implementations such as ArrayList and LinkedList.

2. Traversal Direction

Iterator: • Forward traversal only.

ListIterator: • Forward and backward traversal.

Methods:

• next() • previous()

3. Element Modification

Iterator: • Supports remove() only.

ListIterator: • Supports add() • Supports set() • Supports remove()

4. Index Information

Iterator: • Does not provide element position information.

ListIterator: • Provides:

• nextIndex() • previousIndex()

5. Starting Position

Iterator: • Always starts from the beginning of the collection.

ListIterator: • Can start from any specified index.

Example: Suppose we need to traverse a list in both forward and reverse directions. ListIterator is the better choice.

Code Example:

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class Demo {

    public static void main(String[] args) {

        List<String> technologies =
                new ArrayList<>();

        technologies.add("Java");
        technologies.add("Spring");
        technologies.add("Hibernate");

        ListIterator<String> iterator =
                technologies.listIterator();

        System.out.println("Forward:");

        while (iterator.hasNext()) {

            System.out.println(iterator.next());
        }

        System.out.println("Backward:");

        while (iterator.hasPrevious()) {

            System.out.println(iterator.previous());
        }
    }
}

Output:

Forward:

Java
Spring
Hibernate

Backward:

Hibernate
Spring
Java

Comparison Table:

Iterator:

• Works with all collections • Forward traversal only • remove() supported • No index access • No backward traversal

ListIterator:

• Works only with List • Forward and backward traversal • add(), set(), remove() supported • Index access available • Can start at a specific position

Common Methods:

Iterator:

• hasNext() • next() • remove()

ListIterator:

• hasNext() • next() • hasPrevious() • previous() • add() • set() • remove() • nextIndex() • previousIndex()

When to Use?

Use Iterator: • When simple forward traversal is required. • When working with Set or Queue collections.

Use ListIterator: • When traversing lists in both directions. • When modifying elements during iteration. • When index information is needed.

Interview Tip: A concise interview answer is:

"Iterator supports only forward traversal and works with most collection types. ListIterator extends Iterator and works only with List implementations, providing additional features such as backward traversal, element modification, insertion, replacement, and index-based navigation."