Difference between an Iterator and ListIterator in Java

JavaObject Oriented ProgrammingProgramming

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.KeyIteratorListIterator
1ApplicableIterator 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.
2CallingAs 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.
3Data traverseData traverse in case of the iterator is possible only in one direction as Iterator can traverse in the forward direction onlyList iterator could traverses both in forward and backward directions which makes data traverse in both directions.
4DeletionThe deletion of an element is not allowed in the case of the iterator.ListIterator can replace an element in list collection.
5AdditionThe 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.
6ModificationModification 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.
7Index of elementOne 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

 Live Demo

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
raja
Published on 16-Sep-2019 11:31:47
Advertisements