1. Introduction
Iterator
and ListIterator
are two of the three cursors in Java. Third cursor is Enumeration
. Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics. Both Iterator
and ListIterator
are defined in java.util
package.Iterator
allows to traverse collection elements one by one and only in forward direction.
Following are few important points about ListIterator
:
- A
ListIterator
allows the programmer to traverse the list in forward as well as backward direction. ListIterator
allows to modify the list during iteration and obtain the iterator’s current position in the list.- A
ListIterator
has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call tonext()
. - An iterator for a list of length n has n+1 possible cursor positions as depicted by arrows in diagram:
2. Difference between Iterator and ListIterator
Iterator | ListIterator |
Iterator can traverse the elements of a collection only in forward direction. | ListIterator can traverse the elements of a collection in either direction; forward and backward. |
Iterator can not modify the elements of underlying collection. | ListIterator can modify the elements of underlying collection. |
Iterator can traverse Map, List, Set or Queue. | ListIterator can traverse only List. |
Iterator interface does not provide any method to get index of an element. | ListInterface provides methods to get index. nextIndex() method of ListIterator returns the index of the element that would be returned by a subsequent call to next(). previousIndex() method returns the index of the element that would be returned by a subsequent call to previous(). |
We can not add element to a collection while iterating using Iterator. | We can add element to collection while using ListIterator. |
3. Example
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class ListIteratorExample { public static void main(String[] args) { List<Integer> list = new ArrayList(); list.add(1); list.add(2); list.add(3); System.out.println("List before performing operation"); for(int index=0; index<list.size(); index++) { System.out.println(list.get(index)); } ListIterator<Integer> itr = list.listIterator(); //Here We are trying to add element itr.next(); itr.add(4); System.out.println("List after adding element to list"); for(int index=0; index<list.size(); index++) { System.out.println(list.get(index)); } //Here we are trying to remove element itr.next(); itr.remove(); System.out.println("List after removing element"); for(int index=0; index<list.size(); index++) { System.out.println(list.get(index)); } //Here we are trying to modify element itr.next(); itr.set(5); System.out.println("List after modifying element"); for(int index=0; index<list.size(); index++) { System.out.println(list.get(index)); } } }
Output
List before performing operation 1 2 3 List after adding element to list 1 4 2 3 List after removing element 1 4 3 List after modifying element 1 4 5