ListIterator In Java With Example

Iterator, as we have discussed in earlier topic is for traversing the collection objects and access the elements of  that collection. Basically Iterator is for both List Interface and set Interface.

Now we have a special type of Iterator that is only for List Interface which is known as ListIterator. It is even better Iterator for a List containing more utility methods like getting index of elements and adding elements to the base object.

Using ListIterator we can iterate in both the directions, in forward direction as well as backward direction. 


Table of Contents

Methods of ListIterator

1. void add(E e):
This method inserts the specified element into the list.

2. boolean hasNext():
This method  returns true if this listIterator has more elements when traversing the list in the forward direction.

3. boolean hasPrevious():
This method  returns true if this listIterator has more elements when traversing the list in the backward direction.

4. E next():
This method returns the next element in the list and advances the cursor position one step further.

5. int nextIndex():
This method returns the index of the element that would be returned by a subsequent call to next() method.

6. E previous():
This method returns the previous element in the list and moves the cursor position backwards direction.

7. int previousIndex():
This method returns the index of the element that would be returned by a subsequent call to previous() method.

8. void remove():
This method removes from the list the last element that was returned by next() method or previous() method.

9. void set(E e):
This method replaces the last element returned by next() method or previous() method with the specified element in the argument list


Example of List Iterator

Let us discuss ListIterator with List Implementation with the help of program, following program has been divided into 5 Steps that we will discuss one by one

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class ListIteratorDemo {
public static void main(String a[]){

//Step 1: Object of ArrayList is Created of String Type
List<String> arrayListObj = new ArrayList<String>();

//Step 2 : Elements are added in the array list

arrayListObj.add("This");
arrayListObj.add("is");
arrayListObj.add("Example");
arrayListObj.add("of");
arrayListObj.add("ListIterator");

//Step 3: Obtaining list iterator of type String
ListIterator<String> litr =arrayListObj.listIterator();

//Step 4: Traversing in Forward Direction

System.out.println("Traversing the list in forward direction:");
while(litr.hasNext()){
System.out.println(litr.next());
}

//Step 5: Traversing in Backward Direction
System.out.println("\nTraversing the list in backward direction:");
while(litr.hasPrevious()){
System.out.println(litr.previous());
}
}
}

Output:

Traversing the list in forward direction:

This
is
Example
of
ListIterator

Traversing the list in backward direction:

ListIterator
of
Example
is
This 

Description of Example:

  • In Step 1, we have created an object of List Interface that is of String type.
  • In Step 2, we have used add method to add values in the data structure that we have created in step 1.
  • In Step 3, we have created an object of ListIterator of type String.
  • In Step 4 we have traversed the data structure with while loop using two methods hasNext()and next() in Forward Direction
  • In Step 5 we have traversed the data structure with while loop using two methods hasNext()and next() in Backward Direction

ListIterator Important Points

  • ListIterator is only for List Implementations like ArrayList, LinkedList etc.
  • ListIterator supports Generics, so always use generic type instead using it as Raw Type
  • ListIterator has add as well as remove method which we can use for modifying List Implementations
  • It can traverse in both directions forward as well as Backward.

Leave a Reply

Your email address will not be published. Required fields are marked *