In Java, ListIterator is an interface belongs to java.util package and a member of Java Collection Framework. It is used to obtain a ListIterator object. It provides the following methods that enables bidirectional traversal, element modification, and index tracking within a list (for example, ArrayList, LinkedList, and Vector).
| Method | Description |
|---|---|
| add() | Inserts the specified element into the list at the current cursor position. |
| hasNext() | It returns true if there are more elements when moving forward in the list. |
| hasPrevious() | It returns true if there are more elements when moving backward in the list. |
| nextIndex() | It returns the index of the element that would be returned by next(). |
| next() | It returns the next element in the list and advances the cursor. |
| previousIndex() | It returns the index of the element that would be returned by previous(). |
| previous() | It returns the previous element in the list and moves the cursor backward. |
| remove() | It removes the last element returned by next() or previous(). |
| set() | It replaces the last element returned by next() or previous() with the specified element. |
Java
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class Main {
public static void main(String[] args) {
// Create a List
List fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Mango");
fruits.add("Orange");
// Create ListIterator
ListIterator iterator = fruits.listIterator();
System.out.println("Original List: " + fruits);
// 1. hasNext()
System.out.println("\n--- hasNext() and next() ---");
while (iterator.hasNext()) {
System.out.println("Next Index: " + iterator.nextIndex());
System.out.println("Previous Index: " + iterator.previousIndex());
String fruit = iterator.next();
System.out.println("Element: " + fruit);
}
// 2. hasPrevious() and previous()
System.out.println("\n--- hasPrevious() and previous() ---");
while (iterator.hasPrevious()) {
System.out.println("Previous Index: " + iterator.previousIndex());
String fruit = iterator.previous();
System.out.println("Element: " + fruit);
}
// Move iterator to the first element
iterator.next();
// 3. set()
System.out.println("\n--- set() ---");
iterator.set("Apple Updated");
System.out.println("After set(): " + fruits);
// Move to the next element
iterator.next();
// 4. remove()
System.out.println("\n--- remove() ---");
iterator.remove();
System.out.println("After remove(): " + fruits);
// 5. add()
System.out.println("\n--- add() ---");
iterator.add("New Fruit");
System.out.println("After add(): " + fruits);
// Display final list
System.out.println("\nFinal List: " + fruits);
}
}
Output:
Original List: [Apple, Banana, Mango, Orange] --- hasNext() and next() --- Next Index: 0 Previous Index: -1 Element: Apple Next Index: 1 Previous Index: 0 Element: Banana Next Index: 2 Previous Index: 1 Element: Mango Next Index: 3 Previous Index: 2 Element: Orange --- hasPrevious() and previous() --- Previous Index: 3 Element: Orange Previous Index: 2 Element: Mango Previous Index: 1 Element: Banana Previous Index: 0 Element: Apple --- set() --- After set(): [Apple Updated, Banana, Mango, Orange] --- remove() --- After remove(): [Apple Updated, Mango, Orange] --- add() --- After add(): [Apple Updated, New Fruit, Mango, Orange] Final List: [Apple Updated, New Fruit, Mango, Orange]
We request you to subscribe our newsletter for upcoming updates.