ListIterator Interface

We have seen List interface and its implementation, while discussing about List interface we must also look into ListIterator interface.

ListIterator
List iterator is an interface which allows to traverse in both the directions (forward and backward direction). Also we have to remember few of the important methods.

  1. hasNext
  2. next
  3. hasPrevious
  4. previous
  5. nextIndex
  6. previousIndex

Now we will see the implementation of this ListIterator.


package ArrayList;

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

public class ListIterfaceImplementation {

public static void main(String[] args) {
 // TODO Auto-generated method stub
 List<String> stringList = new ArrayList<>();
 stringList.add("learn");
 stringList.add("from");
 stringList.add("examples");
 
 //ListIterator 
 ListIterator<String> listIterator = stringList.listIterator();
 // Forward direction iteration
 while (listIterator.hasNext()) {
 String string = (String) listIterator.next();
 System.out.println("String is FORWARD DIRECTION: " + string);
 }
 System.out.println("-----------------------------------");
 //Backward direction iteration
 while (listIterator.hasPrevious()) {
 String string = (String) listIterator.previous();
 System.out.println("String is BACKWARD DIRECTION: " + string);
 }
 }
 
}


If we run the above program we will get

String is FORWARD DIRECTION: learn
String is FORWARD DIRECTION: from
String is FORWARD DIRECTION: examples
-----------------------------------
String is BACKWARD DIRECTION: examples
String is BACKWARD DIRECTION: from
String is BACKWARD DIRECTION: learn

In the above program we can able to see that using ListIteartor we can able to iterate in both the directions.

Now we will see the implementation of nextIndex and previousIndex methods

package ArrayList;

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

public class ListIterfaceImplementation {

 public static void main(String[] args) {
 // TODO Auto-generated method stub
 List<String> stringList = new ArrayList<>();
 stringList.add("learn");
 stringList.add("from");
 stringList.add("examples");
 
 //ListIterator 
 ListIterator<String> listIterator = stringList.listIterator();
 // Forward direction iteration
 System.out.println("Forward direction iteration");
 while (listIterator.hasNext()) {
 System.out.println("Next Index is :"+listIterator.nextIndex());
 System.out.println("Previous Index is :"+listIterator.previousIndex());
 String string = (String) listIterator.next();
 }
 
 System.out.println("-----------------------------------");
 System.out.println("Backward direction iteration");
 //Backward direction iteration
 while (listIterator.hasPrevious()) {
 System.out.println("Next Index is :"+listIterator.nextIndex());
 System.out.println("Previous Index is :"+listIterator.previousIndex());
 String string = (String) listIterator.previous();
 }
 }
 
}

Before seeing the output we will first see this image. which will give an idea of how previousIndex and nextIndex will work.

Forward Direction

ListIterator

Backward Direction

ListIterator1

Now we will see the output.

Forward direction iteration
Next Index is :0
Previous Index is :-1
Next Index is :1
Previous Index is :0
Next Index is :2
Previous Index is :1
-----------------------------------
Backward direction iteration
Next Index is :3
Previous Index is :2
Next Index is :2
Previous Index is :1
Next Index is :1
Previous Index is :0

If we see the forward direction iteration, initially the cursor will be between -1 and 0. So for previous index it will print -1 and for next index it will print 0 for first iteration, for second iteration it will print 0 for previous index and 1 for next index and it will continue for the all the further iterations.

Similarly for backward direction iteration, initially the cursor will be between 2 and 3. So the previous index it will print 2 and for next index it will print 3 for first iteration, for second iteration it will print 1 for previous index and 2 for next index and it will continue for all the further iterations.


Leave a comment

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