Skip to content

Java ListIterator Tutorial with Examples

Java ListIterator tutorial with examples will help you understand how to use Java ListIterator in an easy way. ListIterator in Java is a member of the Java Collection Framework. Java ListIterator interface extends the Iterator interface and it allows us to iterate the list in either direction i.e. forward and backward directions.

Unlike Enumeration, Iterator and ListIterator allow us to remove elements from the underlying collection while iterating over the elements of the collection.

Java ListIterator Methods

How to iterate using hasNext and next methods of ListIterator?

The ListIterator allows us to iterate over elements of the collection like List or Set. The hasNext method returns true if there are more elements to iterate in the forward direction. The next method returns the next element of the collection.

Output

Note: The next method throws NoSuchElementException if there is no next element to iterate over. This situation comes when we do not check if there are more elements to iterate over using the hasNext method before calling the next method.

Always make sure that there are elements left to iterate before calling the next method to avoid this exception.

How to iterate in reverse direction using the hasPrevious and previous methods of ListIterator?

The ListIterator also allows us to iterate the collection in the reverse direction or backward direction. To iterate the list or set in the reverse direction, first of all we need to position the ListIterator object at the end of the collection i.e. after the last element. We can do that by using the listIterator method which accepts the index.

After that, we can use the hasPrevious and previous methods of the ListIterator to iterate the elements from the end to the start of the collection.

Output

Similar to the next method, the previous method throws NoSuchElementException if there is no previous element to iterate.

How to remove an element while iterating using ListIterator using the remove method?

The remove method of the ListIterator removes the current element from the underlying collection while iterating.

Output

As you can see from the output, the element is removed from the original List object.

Important notes: The remove method can be called only once per the next or previous method call. Additionally, the remove method cannot be called if the add method has been called after the last call to next or previous method. In either case, the ListIterator throws IllegalStateException exception as shown below.

Output

How to get element index while iterating using ListIterator?

The nextIndex method of the ListIterator returns the index of the next element that will be returned by the subsequent call to the next method. Similarly, the previousIndex method returns the index of the element that will be returned by the subsequent call to the previous method.

Output

Note: If the ListIterator is positioned at the end of the list, the nextIndex method will return the size of the collection. Similarly, if the ListIterator is positioned at the start of the list, the previousIndex method will return -1.

How to replace an element while iterating using ListIterator using the set method?

The set method of the ListIterator replaces an element which was last returned by the next or previous method call with the specified new element.

Output

Important notes about the set method:

1. The set operation is an optional operation. The underlying collection may or may not support it. If the set operation is not supported by the underlying collection, the set method throws UnsupportedOperationException.

2. The set method throws ClassCastException if the type of the new element being added to the collection is not compatible.

3. The set method throws IllegalStateException if the set method is called before calling either the next or previous method. It also throws the same exception if the add or remove method has been called after the last call to next or previous method.

How to add elements while iterating using ListIterator using the add method?

The add method of the ListIterator interface adds an element at the current cursor position. The element is inserted before the element which will be returned by the subsequent next method call. If the list is empty, the element being added will become the only element in the list.

Output

Same as the set method, the add method also throws ClassCastException if the type of the element being added is not compatible. Plus, add operation is also an optional operation, so if the underlying collection does not support adding elements, the add method will throw UnsupportedOperationException exception.

Java ListIterator Examples

References:
Java ListIterator interface Javadoc

Please let me know if you liked the Java ListIterator tutorial with examples in the comments section below.

About the author

Leave a Reply

Your email address will not be published.