Java provided these two interfaces to traverse the data one by one stored in a collection. The internal implementation of iterator and list iterator makes them differ apart but the main agenda of both the iterators is the same.
The following are the important differences between Iterator and ListIterator.
Sr. No. | Key | Iterator | ListIterator |
---|---|---|---|
1 | Applicable | Iterator can be used to traverse any collection irrespective of the type of collection. | List iterator can only be used to iterate only List collection implemented classes like arraylist,linkedlist etc. |
2 | Calling | As mentioned Iterator must be used to enumerate elements in all Collections implemented interfaces like Set, List, Queue, Deque and also in all implemented classes of Map interface. Iterator object can be created by calling iterator() method of Collection interface. | On the other hand, ListIterator must be used when we want to enumerate elements of List.The object of ListIterator can be created by calling listIterator() method present in List interface. |
3 | Data traverse | Data traverse in case of the iterator is possible only in one direction as Iterator can traverse in the forward direction only | List iterator could traverses both in forward and backward directions which makes data traverse in both directions. |
4 | Deletion | The deletion of an element is not allowed in the case of the iterator. | ListIterator can replace an element in list collection. |
5 | Addition | The addition of an element is not allowed in case of an iterator as it throws ConcurrentModificationException. | ListIterator can add an element in list collection any time easily. |
6 | Modification | Modification of an element is not allowed in case of an iterator as it throws ConcurrentModificationException. | ListIterator can modify an element in list collection any time easily by calling set() method. |
7 | Index of element | One can't get the index of the traversed element in collection while using an iterator. | ListIterator has methods like nextIndex() and previousIndex() to obtain indexes of elements at any time while traversing List. |
Example of Iterator vs ListIterator
JavaTester.java
import java.io.*; import java.util.*; public class JavaTester { 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 traversal:"); while (itr.hasNext()) System.out.print(itr.next() + " "); System.out.println(); // ListIterator ListIterator i = list.listIterator(); System.out.println("ListIterator Forward traversal:"); while (i.hasNext()){ System.out.print(i.next() + " "); System.out.println(); System.out.println("ListIterator Backward traversal : "); } while (i.hasPrevious()){ System.out.print(i.previous() + " "); System.out.println(); } } }
Output
Iterator traversal: 1 2 3 4 5 ListIterator Forward traversal: 1 ListIterator Backward traversal : 2 ListIterator Backward traversal : 3 ListIterator Backward traversal : 4 ListIterator Backward traversal : 5 ListIterator Backward traversal : 5 4 3 2 1