Working with Iterator and ListIterator interfaces

Working with the Iterator and ListIterator interfaces in Java provides flexible and efficient ways to traverse and manipulate elements in collections like lists. These interfaces are part of the java.util package and offer different capabilities based on the type of collection they iterate over.

The ListIterator interface extends Iterator and allows bidirectional traversal of a list, including the ability to modify elements and obtain indices. It’s used with classes that implement the List interface (e.g., ArrayList, LinkedList), and provides additional methods for navigation.

How to Obtain a ListIterator

 
ListIterator<Type> listIterator = list.listIterator();

Or, to start from a specific index:

ListIterator<Type> listIterator = list.listIterator(int index);

ListIterator Methods

Method Description
boolean hasNext() Returns true if the list has more elements when moving forward.
E next() Returns the next element in the list.
boolean hasPrevious() Returns true if the list has more elements when moving backward.
E previous() Returns the previous element in the list.
int nextIndex() Returns the index of the element that would be returned by next().
int previousIndex() Returns the index of the element that would be returned by previous().
void remove() Removes the last element returned by next() or previous().
void set(E e) Replaces the last element returned with the specified element.
void add(E e) Inserts the specified element into the list immediately before the element that would be returned by next().
import java.util.ArrayList;
import java.util.Iterator;

public class IteratorExample {
    public static void main(String[] args) {
        ArrayList<String> students = new ArrayList<>();
        students.add("Paani");
        students.add("Mahesh");
        students.add("Datta");
        students.add("Ganesh");
        students.add("Harsha");

        // Using Iterator to iterate over ArrayList
        Iterator<String> iterator = students.iterator();
        while (iterator.hasNext()) {
            String student = iterator.next();
            System.out.println(student);
        }
    }
}
import java.util.LinkedList;
import java.util.ListIterator;

public class ListIteratorExample {
    public static void main(String[] args) {
        LinkedList<String> students = new LinkedList<>();
        students.add("Paani");
        students.add("Mahesh");
        students.add("Datta");
        students.add("Ganesh");
        students.add("Harsha");

        // Using ListIterator to iterate over LinkedList
        ListIterator<String> listIterator = students.listIterator();
        while (listIterator.hasNext()) {
            String student = listIterator.next();
            System.out.println(student);
        }

        // Adding an element in between using ListIterator
        listIterator.add("NewStudent");

        // Iterating in reverse using ListIterator
        System.out.println("\nIterating in reverse:");
        while (listIterator.hasPrevious()) {
            String student = listIterator.previous();
            System.out.println(student);
        }
    }
}
Scroll to Top