Difference between Iterator and ListIterator in Java
๐ก Concept: Iterator vs ListIterator
Iterator and ListIterator are interfaces used to traverse collections in Java, with ListIterator offering bidirectional traversal.
๐ Quick Intro
Iterator allows forward-only traversal, while ListIterator supports forward and backward traversal and element modification.
๐ง Analogy
Think of Iterator as a one-way street allowing cars only forward movement, while ListIterator is a two-way street allowing movement in both directions.
๐ง Technical Explanation
- Iterator works with all Collection types; ListIterator works only with List implementations.
- Iterator supports methods: hasNext(), next(), and remove().
- ListIterator supports hasNext(), next(), hasPrevious(), previous(), add(), set(), and remove().
- ListIterator allows modification during traversal; Iterator allows only removal.
- ListIterator provides index information; Iterator does not.
๐ฏ Use Cases
- โ Use Iterator for simple forward traversal.
- โ Use ListIterator for complex bidirectional traversal and modification.
- โ Use ListIterator when index information is needed during iteration.
๐ป Code Example: Iterator vs ListIterator
import java.util.*;
public class IteratorExample {
public static void main(String[] args) {
List list = new ArrayList<>(Arrays.asList("Java", "Collections", "Framework"));
Iterator iterator = list.iterator();
System.out.println("Iterator traversal:");
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
ListIterator listIterator = list.listIterator();
System.out.println("\nListIterator traversal:");
while(listIterator.hasNext()) {
System.out.println(listIterator.next());
}
System.out.println("Backward traversal:");
while(listIterator.hasPrevious()) {
System.out.println(listIterator.previous());
}
}
}

โ Interview Q&A
Q1: Which interface allows bidirectional traversal?
A: ListIterator.
Q2: Does Iterator support element addition?
A: No.
Q3: Can ListIterator modify elements?
A: Yes.
Q4: Which collections support Iterator?
A: All Collection implementations.
Q5: Is ListIterator limited to Lists?
A: Yes.
Q6: Methods unique to ListIterator?
A: hasPrevious(), previous(), add(), set().
Q7: Can Iterator remove elements?
A: Yes.
Q8: Which is more feature-rich?
A: ListIterator.
Q9: Can Iterator traverse backwards?
A: No.
Q10: Which interface provides index info?
A: ListIterator.
๐ MCQs
Q1. Which interface allows bidirectional traversal?
- Iterator
- ListIterator
- Both
- None
Q2. Does Iterator support element addition?
- Yes
- No
- Sometimes
- Always
Q3. Can ListIterator modify elements?
- No
- Yes
- Sometimes
- Never
Q4. Which collections support Iterator?
- Only Lists
- Only Sets
- All Collection implementations
- None
Q5. Is ListIterator limited to Lists?
- No
- Yes
- Sometimes
- No difference
Q6. Methods unique to ListIterator?
- next(), hasNext()
- hasPrevious(), previous(), add(), set()
- remove()
- All of above
Q7. Can Iterator remove elements?
- No
- Yes
- Sometimes
- Never
Q8. Which is more feature-rich?
- Iterator
- ListIterator
- Both equal
- None
Q9. Can Iterator traverse backwards?
- Yes
- No
- Sometimes
- Always
Q10. Which interface provides index info?
- Iterator
- ListIterator
- Both
- None
๐ก Bonus Insight
ListIterator is a powerful extension to Iterator providing bidirectional traversal and modification capabilities.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!