Difference between Iterator vs ListIterator
Both Iterator and ListIterator are interfaces in Java used to traverse collections, but they have distinct differences in their capabilities and use cases.
Table of Contents
Iterator Introduction
- Iterator is an interface in the java.util package.
- It is used to traverse elements of any collection (e.g., Set, List, Queue).
Methods
- hasNext(): Returns true if the iteration has more elements.
- next(): Returns the next element in the iteration Iterator vs ListIterator.
- remove(): Removes from the underlying collection the last element returned by this iterator (optional operation).
Limitations
- It can traverse the collection only in the forward direction.
- It does not support adding or replacing elements during iteration.
ListIterator Introduction
- ListIterator is an interface in the java.util package.
- It extends Iterator and is specifically designed for traversing lists (List interface implementations like ArrayList, LinkedList).
Methods Iterator vs ListIterator
- hasNext(): Returns true if the iteration has more elements when traversing forward.
- next(): Returns the next element in the iteration when traversing forward.
- hasPrevious(): Returns true if the list iterator has more elements when traversing backward.
- previous(): Returns the previous element in the iteration when traversing backward.
- nextIndex(): Returns the index of the element that would be returned by a subsequent call to next().
- previousIndex(): Returns the index of the element that would be returned by a subsequent call to previous().
- remove(): Removes from the underlying collection the last element returned by next() or previous().
- set(E e): Replaces the last element returned by next() or previous() with the specified element (optional operation).
- add(E e): Inserts the specified element into the list (optional operation).
Advantages Iterator vs ListIterator
- Can traverse the list in both forward and backward directions.
- Supports adding, replacing, and removing elements during iteration.
Example
Here’s an example demonstrating the use of both Iterator and ListIterator with an ArrayList:
java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class IteratorVsListIterator {
public static void main(String[] args) {
// Create an ArrayList and add some elements
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
list.add("Date");
// Using Iterator to traverse the ArrayList
System.out.println("Using Iterator:");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String element = iterator.next();
System.out.println(element);
}
// Using ListIterator to traverse the ArrayList
System.out.println("\nUsing ListIterator (forward):");
ListIterator<String> listIterator = list.listIterator();
while (listIterator.hasNext()) {
String element = listIterator.next();
System.out.println(element);
}
System.out.println("\nUsing ListIterator (backward):");
while (listIterator.hasPrevious()) {
String element = listIterator.previous();
System.out.println(element);
}
// Modifying elements using ListIterator
System.out.println("\nModifying elements using ListIterator:");
listIterator = list.listIterator(); // Reinitialize the ListIterator
while (listIterator.hasNext()) {
String element = listIterator.next();
if (element.equals("Banana")) {
listIterator.set("Blueberry"); // Replace "Banana" with "Blueberry"
}
}
// Display the ArrayList after modification
System.out.println("\nArrayList after modification:");
for (String element : list) {
System.out.println(element);
}
}
}
Explanation Iterator vs ListIterator
- An Iterator is obtained from the ArrayList using the iterator() method.
- The while loop uses hasNext() to check if there are more elements and next() to retrieve each element.
- Output: Using Iterator:
Apple, Banana, Cherry, Date
Using ListIterator (Forward and Backward)
- A ListIterator is obtained from the ArrayList using the listIterator() method.
- The while loop uses hasNext() to check if there are more elements and next() to retrieve each element in the forward direction.
- The while loop uses hasPrevious() to check if there are more elements and previous() to retrieve each element in the backward direction.
- Output: Using ListIterator (forward):
Apple Banana Cherry - Date Using ListIterator (backward):
Date Cherry Banana Apple
Modifying Elements Using Iterator vs ListIterator:
- The ListIterator is reinitialized, and during iteration, the set() method is used to replace “Banana” with “Blueberry”.
- Output: Modifying elements using ListIterator: ArrayList after modification:
Apple Blueberry Cherry Date