Java ListIterator Interface
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.
Methods of ListIterator
- The ListIterator interface contains the following methods:
- add(Object obj): Inserts the specified element into the list immediately before the current element (optional operation).
- hasNext(): Returns true if there are more elements in the list.
- hasPrevious(): Returns true if there are more elements in the list in the reverse direction.
- next(): Returns the next element in the list.
- nextIndex(): Returns the index of the element that would be returned by a subsequent call to next().
- previous(): Returns the previous element in the list.
- previousIndex(): Returns the index of the element that would be returned by a subsequent call to previous().
- remove(): Removes the current element from the list (optional operation).
- set(Object obj): Replaces the current element with the specified element (optional operation).
Advantages of Java ListIterator Interface
Access to index:
The List Iterator interface provides methods like "nextIndex()" and "previousIndex()" that allow you to get the current position of the iterator in the list. This is useful when you need to keep track of the position of elements in the list.
Bidirectional traversal:
Unlike the basic Iterator interface, List Iterator allows you to traverse a list in both forward and backward directions. This means you can easily move to the previous element using the "previous()" method, which is not possible with the Iterator interface.
Modification of elements:
The List Iterator interface provides methods like "set()", "add()", and "remove()" that allow you to modify elements in a list while iterating through it. This is not possible with the basic Iterator interface, which only allows you to remove elements.
Multiple iterators:
You can create multiple List Iterator objects for the same list, and they will each maintain their own iteration state. This means you can have multiple iterations of the same list happening simultaneously.
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
Explanation:
In this program, we create an ArrayList of integers and add some elements to it.
We then obtain a ListIterator object for the list using the listIterator() method of the List interface. We use the hasNext(), next(), hasPrevious(), and previous() methods of the ListIterator interface to traverse the list in both forward and backward directions and print its elements.
We also demonstrate how to modify the list during iteration using the add(), remove(), and set() methods of the ListIterator interface.
Finally, we print the modified list using a for loop and the get() method of the List interface.
Example 2 : Implementation of ListIterator with previous() and previousIndex() methods
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
Explanation:
In this example, we first create an ArrayList of strings and add some elements to it.
We then create a ListIterator object by calling the listIterator() method on the list and passing in the size() of the list as the starting index.
This will create an iterator that starts at the end of the list and moves backwards.
We then use a while loop to iterate over the elements in reverse order using the previous() and previousIndex() methods of the ListIterator.
Inside the loop, we first call previousIndex() to get the index of the current element and then call previous() to get the element itself.
We then print out the index and element using System.out.println().
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
Login/Signup to comment