Iterator and ListIterator are interfaces provided by the Java Collection Framework to traverse collection elements one by one. Iterator is used for simple forward traversal of collections like List, Set, and Queue, whereas ListIterator provides advanced features such as backward traversal, element modification, and insertion in List collections.
- Iterator moves only forward, while ListIterator moves forward and backward.
- Iterator works for all collections, while ListIterator works only for List classes.
Iterator
Iterator is an interface available in the java.util package. It is mainly used to iterate through collection elements one at a time.
- Supports read and remove operations.
- Cannot add or replace elements during traversal.
Syntax
Iterator<Integer> itr = list.iterator();
import java.util.ArrayList;
import java.util.Iterator;
public class IteratorExample {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);
Iterator<Integer> itr = list.iterator();
System.out.println("Elements using Iterator:");
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Output
Elements using Iterator: 10 20 30
Explanation: In this example, the Iterator object traverses the ArrayList in forward direction using hasNext() and next() methods.
ListIterator
ListIterator is an interface that extends Iterator. It is used only for List collections and supports both forward and backward traversal.
- Allows adding, updating, and removing elements.
- Provides index-based methods like nextIndex() and previousIndex().
Syntax
ListIterator<Integer> ltr = list.listIterator();
import java.util.ArrayList;
import java.util.ListIterator;
public class ListIteratorExample {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
ListIterator<Integer> ltr = list.listIterator();
System.out.println("Forward Traversal:");
while (ltr.hasNext()) {
System.out.println(ltr.next());
}
System.out.println("Backward Traversal:");
while (ltr.hasPrevious()) {
System.out.println(ltr.previous());
}
}
}
Output
Forward Traversal: 1 2 3 Backward Traversal: 3 2 1
Explanation: In this example, ListIterator traverses the ArrayList in both forward and backward directions using next() and previous() methods.
Example: ListIterator can help to replace an element whereas Iterator cannot
import java.util.ArrayList;
import java.util.ListIterator;
public class ListIteratorDemo2 {
public static void main(String[] args)
{
ArrayList<Integer> aList
= new ArrayList<Integer>();
aList.add(1);
aList.add(2);
aList.add(3);
aList.add(4);
aList.add(5);
System.out.println("Elements of ArrayList: ");
for (Integer i : aList) {
System.out.println(i);
}
ListIterator<Integer> l
= aList.listIterator();
l.next();
l.set(80000);
System.out.println("\nNow the ArrayList"
+ " elements are: ");
for (Integer i : aList) {
System.out.println(i);
}
}
}
Output
Elements of ArrayList: 1 2 3 4 5 Now the ArrayList elements are: 80000 2 3 4 5
Iterator vs ListIterator
| Iterator | ListIterator |
|---|---|
| Traverses only in forward direction. | Traverses in both forward and backward directions. |
| Works with List, Set, Queue, etc. | Works only with List implementations. |
| Cannot add or replace elements. | Can add, replace, and remove elements. |
| Does not provide index information. | Provides nextIndex() and previousIndex() methods. |
Methods available: next(), hasNext(), remove() | Methods available: next(), previous(), add(), set() |
| Simpler and lightweight. | More powerful and feature-rich. |