Support Online
Skip to main content

Java ListIterator Interface: Back and forth navigation and CRUD operations

🚀 What Will You Learn in This Guide?

In this guide, you will learn the ListIterator interface in Java in detail.
You will see step by step how to perform forward and reverse indexing and insert-update-delete (CRUD) operations in list-based classes.

🧠 Technical Summary

  • Main Topic: Java ListIterator interface.

    oaicite:1

  • Problem Solved: Offering both forward and reverse navigation and CRUD operations instead of iterators that only support forward direction.

  • Steps Followed by the User: Getting a ListIterator object → forward navigation → backward navigation → element addition/update/deletion operations.


1. Basic Features of ListIterator

  • ListIterator only works in List implementations (eg: ArrayList, LinkedList).

    oaicite:2

  • Supports forward and reverse circulation: next() / previous().

  • Allows CRUD (Create, Read, Update, Delete) operations: add(), set(), remove().

  • Can start the cursor before or after a specific element; The cursor is positioned between the elements.

    oaicite:3


2. ListIterator Methods and Brief Descriptions

MethodDescription
void add(E e)Inserts the specified element just before the current position.
boolean hasNext()Returns true if there is a next element in the forward direction.
E next()Returns the forward element and advances the cursor.
int nextIndex()Returns the index of the element to be returned by next().
boolean hasPrevious()Returns true if there is a previous element in the backward direction.
E previous()Returns the element in the reverse direction and moves the cursor back.
int previousIndex()Returns the index of the element to be returned by previous().
void remove()Deletes the item rotated by next() or previous().
void set(E e)Replaces the element returned by next() or previous() with the new value.

3. Using ListIterator: Step by Step

Step 1: Getting a ListIterator Object

List<String> liste = new LinkedList<>();
liste.add("Ali");
liste.add("Veli");
liste.add("Ayşe");
ListIterator<String> iter = liste.listIterator();

This code creates a ListIterator on LinkedList.

Step 2: Forward Roaming


while (iter.hasNext()) {
System.out.println(iter.next());
}

This code prints all the items in the list in order.

Step 3: Reverse Roaming


while (iter.hasPrevious()) {
System.out.println(iter.previous());
}

This code traverses the list from the end to the beginning if the cursor is at the end of the list.

Step 4: Add, Update, Delete Elements


iter = liste.listIterator(); // baştan başlat
while (iter.hasNext()) {
String isim = iter.next();
if (isim.equals("Veli")) {
iter.set("Mehmet"); // Veli yerine Mehmet yazar
iter.add("Zeynep"); // Mehmet’ten sonra Zeynep ekler
}
}

This code replaces a specific element in the list and adds new element.


4. Differences Between Iterator and ListIterator

⚙️ Feature🔁 Iterator🔂 ListIterator
DirectionForward onlyForward and backward
CRUD SupportRead and delete onlyAdd, update, delete
Available ClassesAll CollectionList applications only
Degradability (Spliterator)NoNo (ListIterator does not support parallel execution)

💬 Frequently Asked Questions (FAQ)

  1. Why isn't ListIterator used in the entire Collection interface?

Because it is defined only for List type interfaces; It won't work on all Collections.

  1. What is reverse circulation and what does it do?

Thanks to the hasPrevious() and previous() methods, you can navigate from the end of the list to the beginning; It is suitable for transactions that require reverse ordering.

  1. What do nextIndex() and previousIndex() do?

These methods return the index of the next next() or previous() call; Useful to know the position of the cursor.

  1. Does ListIterator support parallel processing?

No; This structure does not include parallel execution support. Spliterator or Stream API is preferred for such needs.

  1. What should be considered when using ListIterator?

Instead of modifying the list directly, use the ListIterator's add(), set() or remove() methods; otherwise ConcurrentModificationException may occur.


🔚 Result

The Java ListIterator interface provides more flexible control over the List by supporting not only forward but also backward navigation and CRUD operations. When you use ListIterator correctly, you can perform more powerful and readable iteration operations on your collections. You can reinforce your practical knowledge by trying these capabilities on a virtual server you will set up on the GenixNode platform. ☁️