February 1, 2024

Java ListIterator With Examples

In Java there is an Iterator interface which provides an iterator over a collection (List, Set etc.) but there is another interface ListIterator in Java which provides an iterator exclusively for lists like ArrayList, LinkedList, CopyOnWriteArrayList.

While iterator can only move forward, ListIterator provides functionality to traverse the List in either direction; forward or backward, which is one of the difference between Iterator and ListIterator in Java. Other differences are as follows.

  1. Both of these interfaces have remove() method to safely remove an element from List after the iterator is created but ListIterator has an add() method too.
  2. ListIterator also has a set() method to change the element while iterating a List.

No current element in ListIterator

A ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next(). Following image shows the possible cursor positions for a list of length n.

ListIterator in Java

Java ListIterator methods

ListIterator interface in Java provides the following methods-

  1. add(E e)- Inserts the specified element into the list.
  2. hasNext()- Returns true if this list iterator has more elements when traversing the list in the forward direction.
  3. hasPrevious()- Returns true if this list iterator has more elements when traversing the list in the reverse direction.
  4. next()- Returns the next element in the list and advances the cursor position.
  5. nextIndex()- Returns the index of the element that would be returned by a subsequent call to next().
  6. previous()- Returns the previous element in the list and moves the cursor position backwards.
  7. previousIndex()- Returns the index of the element that would be returned by a subsequent call to previous().
  8. remove()- Removes from the list the last element that was returned by next() or previous().
  9. set(E e)- Replaces the last element returned by next() or previous() with the specified element.

Java ListIterator example with bi-directional traversal

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class ListIterationDemo {
  public static void main(String[] args) {
    List<String> carList = new LinkedList<String>();
    carList.add("Audi");
    carList.add("Jaguar");
    carList.add("BMW");
    carList.add("Mini Cooper");
    //Getting ListIterator
    ListIterator<String> ltr = carList.listIterator();
    //forward iteration
    System.out.println("List iteration - forward direction");
    while(ltr.hasNext()){
        System.out.println(ltr.next());
    }
    // backward iteration 
    System.out.println("List iteration - backward direction");
    while(ltr.hasPrevious()){
        System.out.println(ltr.previous());
    }
  }
}
Output
List iteration - forward direction
Audi
Jaguar
BMW
Mini Cooper
List iteration - backward direction
Mini Cooper
BMW
Jaguar
Audi

Example using add() and set() method of ListIterator

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class ListIterationDemo { 
  public static void main(String[] args) {
    List<String> carList = new LinkedList<String>();
    carList.add("Audi");
    carList.add("Jaguar");
    carList.add("BMW");
    carList.add("Mini Cooper");
    //Getting ListIterator
    ListIterator<String> ltr = carList.listIterator(); 
    while(ltr.hasNext()){
      String car = ltr.next();
      
      if(car.equals("BMW")) {
          ltr.add("Mercedes");
      }
      if(car.equals("Mini Cooper")) {
          ltr.set("Camry");
      }
    }
    System.out.println("List elements- " + carList);
  }
}
output
List elements- [Audi, Jaguar, BMW, Mercedes, Camry]

That's all for the topic Java ListIterator With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment