ListIterator is one of the four java cursors. It is a java iterator which 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.
Hierarchy of ListIterator
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 direction.
- It supports all the four CRUD operations(Create, Read, Update, Delete) operations.
Interesting Fact about ListIterator
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.
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:
This returns the list iterator of all the elements of the list.
Welcome To Gfg Welcome To Gfg
ListIterator is a bi-directional iterator. For this functionality, it has two kinds of methods:
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 backwards.
- 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:
Forward Direction Iteration: learn from Geeksforgeeks Backward Direction Iteration: Geeksforgeeks from learn
ArrayList Iterator methods
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 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 previous would return the element with the specified index minus one. The returned list iterator is fail-fast.
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()).
- 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 which are easy to use.
- This iterator is only for list implementation classes.
- Not a universal cursor.
- It is not applicable for all collection API.
- Parallel iteration of elements is not supported by list Iterator.
- listiterator does not support the good performance of numerous elements iteration.
Iterator v/s ListIterator
- They both are introduced in java 1.2.
- They both are used to iteration lists.
- They both support forward direction traversal.
- They both supports READ and DELETE operations.
Differences: Iterator ListIterator
It can traverse a collection of any type. It traverses only list collection implemented classes like LinkedList, ArrayList etc. Traversal can only be done in forward direction. Traversal of elements can be done in both forward and backward direction. Iterator object can be created by calling iterator() method of the collection class. ListIterator object can be created by calling directions listIterator() method of the collection class. Deletion of elements is not allowed. Deletion of elements is allowed. It throws ConcurrentModificationException on doing addition operation. Hence, addition is not allowed. Addition of elements is allowed. In iterator, we can’t access the index of the traversed element. In listIterator, we have nextIndex() and nextPrevious() methods for accessing the indexes of the traversed or the next traversing element. Modification of any element is not allowed. Modification is allowed.
Methods of ListIterator
|void add(E e)||This method inserts the specified element in the list.|
|boolean hasNext(),||This returns true if the list has more elements to traverse.|
|boolean hasPrevious()||This returns true if the list iterator has more elements while traversing the list in the backward direction.|
|E next()||This method returns the next element and increases the cursor by one position.|
|int nextIndex()||This method returns the index of the element which would be returned on calling the next() method.|
|E previous()||This method returns the previous element of the list and shifts the cursor one position backwards.|
|int previousIndex()||This method returns the index of the element which would be returned on calling the previous() method.|
|void remove()||This method removes the last element from the list that was returned on calling next() or previous() method element from.|
|void set(E e)||This method replaces the last element that was returned on calling next() or previous() method with the specified element.|
Methods declared in interface java. util.Iterator
|default void forEachRemaining(Consumer<? super E> action)||Performs the given action for each remaining element until all elements have been processed or the action throws an exception.|
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.