1. Java ListIterator – Introduction
Like Iterator, the Java ListIterator also a special reference over the collection of objects. In the past example on Java Iterator, we saw that it can only remove the collection items. But, the ListIterator can remove as well as add items to the collection while iteration is in progress. Unlike the Iterator, the ListIterator can iterate the collection in both forward and backward directions. So, the ListIterator is called a Bi-Directional Cursor.
In this example, we will use the ArrayList of Fruits and learn how ListIterator works. Let us start.
2. Java’s ListIterator Methods
Let us say we have an ArrayList of six items and ListIterator is pointing at the item 103 as shown in the below picture:

Recall that a ListIterator can move in both the directions. So, the hasNext method will tell whether a call to the next method with succeed or not by returning true or false. The same way, hasPrevious method will tell call to previous method will succeed or not.
We already know how the next method works when we explored the Java Iterator. The same way it works for ListIterator as well. The previous method of the ListIterator retrieves the element at the current location and moves the cursor reference in reverse direction. Note, from the above picture, the call to next and previous both returns the element 103. But it differs in the direction move. This is depicted below:

Now let us see an example to learn ListIterator.
3. ListIterator Example
3.1 Iterate in the Forward Direction
First, we create ArrayList
with six fruits in it. Below is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//Sample 01: Iterators import java.util.ArrayList; import java.util.ListIterator; //Sample 02: Create an Array List ArrayList<String> Fruits = new ArrayList<String>(); Fruits.add("Apple"); Fruits.add("Orange"); Fruits.add("Banana"); Fruits.add("Orange"); Fruits.add("Apple"); Fruits.add("Banana"); Fruits.add("Apple"); Fruits.add("Orange"); Fruits.add("Orange"); Fruits.add("Banana"); |
Next, we write a function printList
and the function iterate through the ArrayList
in the forward direction using the
hasNext() and
next() functions. This is same as how we used the Iterator in the example Java Iterator. Note, at line 6, we used the
listIterator() function on the ArrayList
to get the Bi-Directional Cursor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//Sample 03: Iterate Over as List as ListIterator private static void printList(ArrayList<String> Fruits) { System.out.println("The Fruit Lists:"); //3.1 Iterate Over the List ListIterator<String> itr = Fruits.listIterator(); while(itr.hasNext()) { String str = itr.next(); System.out.println(str); } System.out.println("================"); } //Sample 04: Iterate and Print the Fruits printList(Fruits); |
3.2 Iterate the ArrayList Collection in Reverse Direction
We can iterate the collection in reverse order using the function
hasPrevious() and
previous(). The below function, iterateRemove
takes two arguments. First one is the ArrayList
and second one is the fruit name which needs to be cut out in the ArrayList
. The below code at line number 13 uses ListIreator’s
remove method to remove the current item from the collection. First, we check the current item in the collection with the passed-in parameter. When it matches, we remove it from the collection. Note, the
ListIterator also supports
add method and we can add a new item to the collection while iteration is in progress.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//Sample 05: Iterate Over Array List in Reverse Direction private static void iterateRemove(ArrayList<String> Fruits, String FruitName) { //5.1 Get the Iterator ListIterator<String> itrbck = Fruits.listIterator(Fruits.size()); //5.2 Iterate Over the ArrayList in Backward Direction while(itrbck.hasPrevious()) { String str = itrbck.previous(); if (str.equals(FruitName)) itrbck.remove(); //Same way we Can add also } } //Sample 06: Now we will Remove Apple using ListIterator iterateRemove(Fruits, "Apple"); printList(Fruits); |
3.3 Replace Collection Item using ListIterator
We can replace an item using the
set method of the ListIterator. In the below code, our custom function takes three arguments. First one is the ArrayList
, second param tells what fruit we want to replace and the third one is the string that overwrites the fruit in the collection. The function iterates the fruits in a forward direction. In each iteration, it checks the fruit matches with the second param. When a match occurs, it uses the
set function (Line No 13) and replaces the fruit.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//Sample 07: Iterate and Replace Elements private static void iterateReplace(ArrayList<String> Fruits, String FruitName, String NewFruit) { //7.1 Get the Iterator ListIterator<String> itr = Fruits.listIterator(); //7.2 Iterate Over the ArrayList while(itr.hasNext()) { String str = itr.next(); if (str.equals(FruitName)) itr.set(NewFruit); } } //Sample 08: Replace the Fruits iterateReplace(Fruits, "Orange", "Banana"); printList(Fruits); |
4. ListIterator – Complete Code Example
Below is the complete code example for Java ListIterator:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
//Sample 01: Iterators import java.util.ArrayList; import java.util.ListIterator; public class MainEntry { public static void main(String[] args) { //Sample 02: Create an Array List ArrayList<String> Fruits = new ArrayList<String>(); Fruits.add("Apple"); Fruits.add("Orange"); Fruits.add("Banana"); Fruits.add("Orange"); Fruits.add("Apple"); Fruits.add("Banana"); Fruits.add("Apple"); Fruits.add("Orange"); Fruits.add("Orange"); Fruits.add("Banana"); //Sample 03: Iterate and Print the Fruits printList(Fruits); //Sample 05: Now we will Remove Apple using ListIterator iterateRemove(Fruits, "Apple"); printList(Fruits); //Sample 07: Replace the Fruits iterateReplace(Fruits, "Orange", "Banana"); printList(Fruits); } //Sample 04: Iterate Over as List as ListIterator private static void printList(ArrayList<String> Fruits) { System.out.println("The Fruit Lists:"); //4.1 Iterate Over the List ListIterator<String> itr = Fruits.listIterator(); while(itr.hasNext()) { String str = itr.next(); System.out.println(str); } System.out.println("================"); } //Sample 06: Iterate Over Array List in Reverse Direction private static void iterateRemove( ArrayList<String> Fruits, String FruitName) { //6.1 Get the Iterator ListIterator<String> itrbck = Fruits.listIterator(Fruits.size()); //6.2 Iterate Over the ArrayList in Backward Direction while(itrbck.hasPrevious()) { String str = itrbck.previous(); if (str.equals(FruitName)) itrbck.remove(); //Same way we Can add also } } //Sample 08: Iterate and Replace Elements private static void iterateReplace( ArrayList<String> Fruits, String FruitName, String NewFruit) { //8.1 Get the Iterator ListIterator<String> itr = Fruits.listIterator(); //8.2 Iterate Over the ArrayList while(itr.hasNext()) { String str = itr.next(); if (str.equals(FruitName)) itr.set(NewFruit); } } } |
5. Watch this Example as a YouTube Demo
Categories: Java
Tags: Collection::listIterator, ListIterator::hasNext, ListIterator::hasPrevious, ListIterator::next, ListIterator::previous