Difference between an Iterator and ListIterator in Java
Last Updated :
11 Jul, 2025
Iterators are used in Collection framework in Java to retrieve elements one by one. It can be applied to any Collection object. By using Iterator, we can perform both read and remove operations. Iterator must be used whenever we want to enumerate elements in all Collection framework implemented interfaces like Set, List, Queue, Deque and also in all implemented classes of Map interface. Iterator is the only cursor available for entire collection framework. Iterator object can be created by calling iterator() method present in Collection interface.
// Here "c" is any Collection object. itr is of
// type Iterator interface and refers to "c"
Iterator itr = c.iterator();
ListIterator It is only applicable for List collection implemented classes like arraylist, linkedlist etc. It provides bi-directional iteration. ListIterator must be used when we want to enumerate elements of List. This cursor has more functionality(methods) than iterator. ListIterator object can be created by calling listIterator() method present in List interface.
// Here "l" is any List object, ltr is of type
// ListIterator interface and refers to "l"
ListIterator ltr = l.listIterator();
Differences between Iterator and ListIterator:
- Iterator can traverse only in forward direction whereas ListIterator traverses both in forward and backward directions. Example:
Java
import java.io.*;
import java.util.*;
class IteratorDemo1 {
public static void main(String[] args)
{
ArrayList<Integer> list
= new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
// Iterator
Iterator itr = list.iterator();
System.out.println("Iterator:");
System.out.println("Forward traversal: ");
while (itr.hasNext())
System.out.print(itr.next() + " ");
System.out.println();
// ListIterator
ListIterator i = list.listIterator();
System.out.println("ListIterator:");
System.out.println("Forward Traversal : ");
while (i.hasNext())
System.out.print(i.next() + " ");
System.out.println();
System.out.println("Backward Traversal : ");
while (i.hasPrevious())
System.out.print(i.previous() + " ");
System.out.println();
}
}
Output:Iterator:
Forward traversal:
1 2 3 4 5
ListIterator:
Forward Traversal :
1 2 3 4 5
Backward Traversal :
5 4 3 2 1
- ListIterator can help to replace an element whereas Iterator cannot. Example:
Java
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
- OUTPUT Table showing Difference between Iterator and ListIterator
Iterator | ListIterator |
---|
Can traverse elements present in Collection only in the forward direction. | Can traverse elements present in Collection both in forward and backward directions. |
Helps to traverse Map, List and Set. | Can only traverse List and not the other two. |
Indexes cannot be obtained by using Iterator. | It has methods like nextIndex() and previousIndex() to obtain indexes of elements at any time while traversing List. |
Cannot modify or replace elements present in Collection | We can modify or replace elements with the help of set(E e) |
Cannot add elements and it throws ConcurrentModificationException. | Can easily add elements to a collection at any time. |
Certain methods of Iterator are next(), remove() and hasNext(). | Certain methods of ListIterator are next(), previous(), hasNext(), hasPrevious(), add(E e). |
Similar Reads
Replace an Element From ArrayList using Java ListIterator To replace an element from an ArrayList, the set() method of ListIterator interface can be used. set() method of ListIterator replaces the last element which is returned by the next() or previous() methods, along with the given element. Two ways of replacing the elements using ListIterator shown bel
3 min read
Iterate a LinkedList in Reverse Order in Java For traversing a linked list in reverse order we can use Descending Iterator or List Iterator 1. Descending Iterator Syntax: LinkedList<String> linkedlist = new LinkedList<>(); Iterator<String> listIterator = linkedlist.descendingIterator(); Returns: Descending Iterator returns the
2 min read
Java Program to Add an Element to ArrayList using ListIterator In this article, we will learn how to add an element to ArrayList using ListIterator. The ListIterator is used to return a list iterator over the list elements. The listIterator() returns an iterator over the list from the beginning, but the listIterator(index) returns an iterator over the list from
2 min read
Converting LinkedHashSet to Different Collection Types in Java In Java, LinkedHashSet is a Class in the Java Collections Framework that extends the Collection HashSet and maintains the insertion order of the elements. The underlined data structure of the LinkedHastSet is that the insertion order is preserved. Converting LinkedHashSet to Other CollectionsIn cert
4 min read
Traverse Through ArrayList in Forward Direction in Java ArrayList is a part of the collection framework and is present in java.util package. It provides us with dynamic arrays in Java. The listIterator() method of java.util.ArrayList class is used to return a list iterator over the elements in this list (in a proper organized sequence). ArrayList can be
3 min read
Iterate List in Java using Loops In this article, we are going to see how to iterate through a List. In Java, a List is an interface of the Collection framework. List can be of various types such as ArrayList, Stack, LinkedList, and Vector. There are various ways to iterate through a java List but here we will only be discussing ou
7 min read