Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Java ListIterator Explained

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:

ListIterator is Bi-Directional
ListIterator is Bi-Directional

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:

ListIterator is Bi-Directional
ListIterator is Bi-Directional

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:

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.

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.

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.

4. ListIterator – Complete Code Example

Below is the complete code example for Java ListIterator:

5. Watch this Example as a YouTube Demo

Categories: Java

Tags: , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.