5 Difference between Iterator and ListIterator in Java with Example

In this tutorial we will learn about the difference between Iterator and ListIterator . We will also look at the examples of Iterator and ListIterator one by one. We have already discussed different type of iterators  i.e fail-fast iterator and fail-safe iterator.

Difference between Iterator and ListIterator in Java with Example

1. Traversal Direction  :  ListIterator allows the programmers to iterate the list objects in both directions i.e forward as well as backward direction using previous() and next() method.
Iterator can be used to  iterate the list,map and set object in one direction i.e forward.

2.  Set and Map implemented Objects Traversal : ListIterator can be used to traverse List object only . But Iterator can be used to traverse Map, List and Set implemented objects.


for example
// ListIterator object is created
ListIterator listIteratorObject = List.listIterator();


// Iterator object is created

Iterator  iteratorObject  = List.iterator();




3. Add or Set operation at any index : According to ListIterator Oracle docs,
ListIterator can modify the list  during iteration using add(E e) , remove() or set(E e).
Iterator can not add the element during traversal but they can remove the element from the underlying collection during the iteration as they only consist of remove() method. There is no add(E e) and set(E e) method in Iterator.

4. Determine Iterator's current position :  ListIterator can obtain the iterator's current position in the list. Iterator's current position during traversal can not be determined using Iterator.

5. Retrieve Index of the element : ListIterator can obtain the index of the elements using previousIndex(E e) or nextIndex(E e) methods. We can not obtain the index using Iterator as there is no such methods present.

Example of Iterator and ListIterator 


import java.util.Iterator;
import java.util.ListIterator;
public class IteratorListIteratorExample { public static void main(String[] args) { 
 
           
  
        List listObject = new ArrayList();
        listObject.add("Alive is awesome");
        listObject.add("Love yourself");

        ListIterator listIteratorObject =  listObject.listIterator();
        System.out.println("ListIterator object output in forward direction:");
        System.out.println("");

       
         while( listIteratorObject.hasNext() )
         {
           System.out.println(listIteratorObject.next());
         }
      
        System.out.println("ListIterator object output in backward direction:");
        System.out.println("");

      
         while( listIteratorObject.hasPrevious() )
         {
           System.out.println(listIteratorObject.previous());
         }
         
 
        List iteratorListObject = new ArrayList();

        iteratorListObject.add("Facebook");  
        iteratorListObject.add("Google");
        iteratorListObject.add("Apple");
        
        Iterator javaHungryIterator =  iteratorListObject.iterator();
        System.out.println("Iterator object output in forward direction:");

        while( javaHungryIterator.hasNext() )
        {
           System.out.println(javaHungryIterator.next());
        }
 
 }
}


Output :

ListIterator object output in forward direction:
Alive is awesome
Love yourself
ListIterator object output in backward direction:
Love yourself
Alive is awesome

Iterator object output in forward direction:

Facebook
Google
Apple


Similarities between Iterator and ListIterator in Java

1. Interfaces : Both Iterator and ListIterator are interfaces . ListIterator extends Iterator interface.

Difference between Iterator and ListIterator in java with Example2. Collection Framework : Both Iterator and ListIterator are member of the Java Collection Framework.

3. Traversal : Both are used to iterate over the collection of objects .

4. Interfaces added to jdk : Both interfaces are added to the jdk in java 1.2



Recap : Difference between Iterator and ListIterator in Java with Example 


ListIteratorIterator
Traversal DirectionBoth , forward and backwardForward
Objects traversalList only Map, Set and List 
Add and Set operationsAllows both operationsNot possible
Iterator's current positionYes , can be determinedNot possible.
Retrieve IndexYesNot possible


In case you have any questions regarding the difference between Iterator and ListIterator in Java with example , please feel free to mention in the comments.



About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry