Java ListIterator Interface

Java List Iterator

What is Java Listinterface?

  • The ListIterator interface in Java is a sub-interface of the Iterator interface and is used to iterate over elements in a list in both forward and backward directions.
  • The ListIterator interface provides additional functionality over the basic Iterator interface, such as the ability to modify the list during iteration, traverse the list in both directions, and obtain the index of the current element.
  • To understand the Java ListIterator Interface, Read the Complete Article.

How to create ListIterator object in Java?

  • Here are the Steps to Create ListIterator object In Java
    • Create a List object: You need to create a List object before you can create a ListIterator object. You can use any class that implements the List interface, such as ArrayList, LinkedList, or Vector.
    • Add elements to the list: You can add elements to the list using the add() method.
    • Create a ListIterator object: You can create a ListIterator object using the listIterator() method of the List interface.
    • Traverse the list using the ListIterator: Once you have created the ListIterator object, you can use it to traverse the list.
    • You can use the has Next() method to check if there are more elements in the list, and the next() method to get the next element in the list.
    • You can also use the has Previous() method and the previous() method to traverse the list in reverse order.
Java ListIterator

Advantages of Java ListIterator Interface

Let’s look at the Java Writer Using FileWriter Method to perform certain operations.

Example 1: Implementation of ListIterator

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

public class Main
{
  public static void main (String[]args)
  {
    // Create a list of integers
    List < Integer > myList = new ArrayList < Integer > ();
    myList.add (10);
    myList.add (20);
    myList.add (30);
    myList.add (40);

    // Get a ListIterator object for the list
    ListIterator < Integer > iterator = myList.listIterator ();

    // Traverse the list in forward direction and print its elements
    System.out.println ("Traversing the list in forward direction:");
    while (iterator.hasNext ())
      {
	int element = iterator.next ();
	  System.out.println (element);
      }

    // Traverse the list in backward direction and print its elements
    System.out.println ("Traversing the list in backward direction:");
    while (iterator.hasPrevious ())
      {
	int element = iterator.previous ();
	System.out.println (element);
      }

    // Modify the list during iteration
    System.out.println ("Modifying the list during iteration:");
    while (iterator.hasNext ())
      {
	int element = iterator.next ();
	if (element == 20)
	  {
	    iterator.remove ();
	  }
	if (element == 30)
	  {
	    iterator.add (35);
	  }
	if (element == 40)
	  {
	    iterator.set (45);
	  }
      }

    // Print the modified list
    System.out.println ("Modified list:");
    for (int i = 0; i < myList.size (); i++)
      {
	System.out.println (myList.get (i));
      }
  }
}

Output

Traversing the list in forward direction:
10
20
30
40
Traversing the list in backward direction:
40
30
20
10
Modifying the list during iteration:
Modified list:
10
30
35
45

Example 2 : Implementation of ListIterator with previous() and previousIndex() methods 

Run

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

public class Main
{
  public static void main (String[]args)
  {
    List < String > list = new ArrayList <> ();
    list.add ("Apple");
    list.add ("Banana");
    list.add ("Cherry");
    list.add ("Durian");

    ListIterator < String > iterator = list.listIterator (list.size ());

    // iterate over the elements in reverse order using previous() and previousIndex()
    while (iterator.hasPrevious ())
      {
	int index = iterator.previousIndex ();
	String element = iterator.previous ();
	  System.out.println ("Element at index " + index + " is " + element);
      }
  }
}

Output

Element at index 3 is Durian
Element at index 2 is Cherry
Element at index 1 is Banana
Element at index 0 is Appl

Limitations of ListIterator

Although ListIterator is the most potent cursor, it still has significant drawbacks. These are what they are:

  • Only list-implemented class objects can use the Java List Iterator. As a result, it is not a generic Java cursor.
  • That does not apply to APIs for entire collections.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription