ListIterator in Java

31 May, 2022
ListIterator is one of the four java cursors. It is a java iterator that is used to traverse all types of lists including ArrayList, Vector, LinkedList, Stack, etc. It is available since Java 1.2. It extends the iterator interface. The hierarchy of ListIterator is as follows: 
Some Important points about ListIterator
It is useful for list implemented classes.Available since java 1.2.It supports bi-directional traversal. i.e both forward and backward directions.It supports all the four CRUD operations(Create, Read, Update, Delete) operations.
It is useful for list implemented classes.
Available since java 1.2.
It supports bi-directional traversal. i.e both forward and backward directions.
It supports all the four CRUD operations(Create, Read, Update, Delete) operations.
There is no current element in ListIterator. Its cursor always lies between the previous and next elements. The previous() will return to the previous elements and the next() will return to the next element. Therefore, for a list of n length, there are n+1 possible cursors.
Syntax: Declaration
public interface ListIterator<E> extends Iterator<E>
Where E represents the generic type i.e any parameter of any type/user-defined object.
Syntax: To get a list Iterator on a list
ListIterator<E> listIterator()  
This returns the list iterator of all the elements of the list.
Example:
Java
// Java Program to Show the Usage of listIterator import java.util.*; public class ListIteratorDemo {       // Main driver method    public static void main(String[] args)    {        // Creating a list of names        List<String> names = new LinkedList<>();               names.add("Welcome");        names.add("To");        names.add("Gfg");         // Getting ListIterator        ListIterator<String> namesIterator            = names.listIterator();         // Traversing elements using next() method        while (namesIterator.hasNext()) {            System.out.println(namesIterator.next());        }         // for-each loop creates Internal Iterator here.        for (String s : names) {            System.out.println(s);        }    }}
Welcome
To
Gfg
Welcome
To
Gfg
1. Forward direction iteration
hasNext(): This method returns true when the list has more elements to traverse while traversing in the forward direction
next(): This method returns the next element of the list and advances the position of the cursor.
nextIndex(): This method returns the index of the element that would be returned on calling the next() function.
2. Backward direction iteration
hasPrevious(): This method returns true when the list has more elements to traverse while traversing in the reverse direction
previous(): This method returns the previous element of the list and shifts the cursor one position backward.
previousIndex(): This method returns the index of the element that would be returned on calling the previous() function.
Example code showing both forward and backward direction iterations using list Iterator:
Java
// Java Program to traverse the list both in forward and// backward direction using listIterator import java.util.*; public class GFG {       public static void main(String[] args)    {          // list of names        List<String> names = new LinkedList<>();        names.add("learn");        names.add("from");        names.add("Geeksforgeeks");         // Getting ListIterator        ListIterator<String> listIterator            = names.listIterator();         // Traversing elements        System.out.println("Forward Direction Iteration:");        while (listIterator.hasNext()) {            System.out.println(listIterator.next());        }         // Traversing elements, the iterator is at the end        // at this point        System.out.println("Backward Direction Iteration:");        while (listIterator.hasPrevious()) {            System.out.println(listIterator.previous());        }    }}
Forward Direction Iteration:
learn
from
Geeksforgeeks
Backward Direction Iteration:
Geeksforgeeks
from
learn
A. listIterator(): The listIterator() method of java.util.ArrayList class is used to return a list iterator over the elements in this list (in proper sequence). The returned list iterator is fail-fast.
Syntax:
public ListIterator listIterator()
Return Value: This method returns a list iterator over the elements in this list (in proper sequence).
B. listIterator(int index)
This listIterator(int index) method is used to return a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. The specified index indicates the first element that would be returned by an initial call to next. An initial call to the previous would return the element with the specified index minus one. The returned list iterator is fail-fast.
Syntax:
public ListIterator listIterator(int index)
Parameters: This method takes the index of the first element as a parameter to be returned from the list iterator (by a call to next)
Return Value: This method returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
Exception: This method throws IndexOutOfBoundsException if the index is out of range (index size()).
Advantages:
It supports all four CRUD (Create, Read, Update, Delete) operations.
It supports Bi-directional traversing i.e both forward and backward direction iteration.
Simple method names are easy to use.
Limitations:
This iterator is only for list implementation classes.
Not a universal cursor.
It is not applicable to all collection API.
Parallel iteration of elements is not supported by the list Iterator.
listiterator does not support the good performance of numerous elements iteration.
Similarities:
They both are introduced in java 1.2.
They both are used for iteration lists.
They both support forward direction traversal.
They both supports READ and DELETE operations.
Differences:
                Iterator
                                           ListIterator
Method
Description
Method
Description
arcsinrad3
sankarsanpakhira
Java-Iterator
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
Stream In Java
ArrayList in Java
Collections in Java
Singleton Class in Java
Multidimensional Arrays in Java
Set in Java