Java Iterator and ListIterator – Complete Guide with Examples


Learn how to traverse Java collections using Iterator and ListIterator, their differences, methods, and practical examples for List, Set, and Map traversal.

Iterator and ListIterator in Java – Complete Detailed Tutorial

In Java, collections can be traversed using Iterator and ListIterator. They provide a standard way to iterate over elements without exposing the underlying structure.

1. Iterator Interface

  1. Iterator is used to traverse any Collection (List, Set, etc.)
  2. Supports forward-only traversal
  3. Allows removal of elements during iteration
  4. Part of java.util package

Common Methods:

MethodDescription
hasNext()Checks if next element exists
next()Returns next element
remove()Removes current element

Example – Using Iterator with ArrayList


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Main {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");

Iterator<String> it = fruits.iterator();
System.out.println("Fruits:");
while(it.hasNext()) {
String fruit = it.next();
System.out.println(fruit);
if(fruit.equals("Banana")) {
it.remove(); // remove Banana
}
}
System.out.println("After removal: " + fruits);
}
}

Output:


Fruits:
Apple
Banana
Orange
After removal: [Apple, Orange]

Key Points:

  1. Can traverse List, Set, or any Collection
  2. Supports element removal but not modification

2. ListIterator Interface

  1. ListIterator is a bidirectional iterator for List implementations (ArrayList, LinkedList)
  2. Supports forward and backward traversal
  3. Can add, remove, or modify elements during iteration

Common Methods:

MethodDescription
hasNext()Checks if next element exists
next()Returns next element
hasPrevious()Checks if previous element exists
previous()Returns previous element
add(E e)Adds element at current position
set(E e)Replaces last returned element
remove()Removes last returned element

Example – Using ListIterator with LinkedList


import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class Main {
public static void main(String[] args) {
List<String> fruits = new LinkedList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");

ListIterator<String> listIt = fruits.listIterator();

System.out.println("Forward traversal:");
while(listIt.hasNext()) {
String fruit = listIt.next();
System.out.println(fruit);
if(fruit.equals("Banana")) {
listIt.set("Grapes"); // replace Banana with Grapes
}
}

System.out.println("Backward traversal:");
while(listIt.hasPrevious()) {
System.out.println(listIt.previous());
}

System.out.println("Modified List: " + fruits);
}
}

Output:


Forward traversal:
Apple
Banana
Orange
Backward traversal:
Orange
Grapes
Apple
Modified List: [Apple, Grapes, Orange]

Key Points:

  1. Works only with List implementations
  2. Allows bidirectional traversal
  3. Supports modification, addition, and removal

3. Iterator vs ListIterator

FeatureIteratorListIterator
TraversalForward onlyForward and backward
Collection typeAll CollectionsOnly List
ModifyCan remove onlyCan add, remove, replace
Position controlNoYes (nextIndex, previousIndex)

4. Key Points

  1. Iterator: simple forward traversal, removal allowed
  2. ListIterator: advanced, bidirectional, can modify elements
  3. Both avoid exposing underlying collection structure

5. Summary

  1. Iterator and ListIterator are core for safe collection traversal
  2. Choose Iterator for generic traversal
  3. Choose ListIterator for Lists needing bidirectional traversal or modification
  4. Always use iterator methods instead of modifying collection directly