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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
List<String> listWeekDays = new ArrayList<String>(); listWeekDays.add("Monday"); listWeekDays.add("Tuesday"); listWeekDays.add("Wednesday"); listWeekDays.add("Thursday"); listWeekDays.add("Friday"); //get the list iterator ListIterator<String> listIterator = listWeekDays.listIterator(); //iterate using hasNext and next methods while(listIterator.hasNext()){ System.out.println( listIterator.next() ); } |
Output
1 2 3 4 5 |
Monday Tuesday Wednesday Thursday Friday |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
List<String> listWeekDays = new ArrayList<String>(); listWeekDays.add("Monday"); listWeekDays.add("Tuesday"); listWeekDays.add("Wednesday"); listWeekDays.add("Thursday"); listWeekDays.add("Friday"); /* * get the ListIterator and position it * at the end of the collection. * * Use the size of the collection to do that */ ListIterator<String> listIterator = listWeekDays.listIterator( listWeekDays.size() ); /* * iterate in backward direction using hasPrevious * and previous methods */ while(listIterator.hasPrevious()){ System.out.println( listIterator.previous() ); } |
Output
1 2 3 4 5 |
Friday Thursday Wednesday Tuesday Monday |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
List<String> listWeekDays = new ArrayList<String>(); listWeekDays.add("Monday"); listWeekDays.add("Tuesday"); listWeekDays.add("Wednesday"); listWeekDays.add("Thursday"); listWeekDays.add("Friday"); ListIterator<String> listIterator = listWeekDays.listIterator(); String strDay = null; while( listIterator.hasNext() ){ //get the current element strDay = listIterator.next(); if(strDay.equals("Wednesday")){ //remove an element using remove method listIterator.remove(); } } System.out.println("List contains: " + listWeekDays); |
Output
1 |
List contains: [Monday, Tuesday, Thursday, Friday] |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
List<String> listWeekDays = new ArrayList<String>(); listWeekDays.add("Monday"); listWeekDays.add("Tuesday"); listWeekDays.add("Wednesday"); listWeekDays.add("Thursday"); listWeekDays.add("Friday"); ListIterator<String> listIterator = listWeekDays.listIterator(); String strDay = null; while( listIterator.hasNext() ){ strDay = listIterator.next(); listIterator.remove(); if(strDay.equals("Monday")){ /* * this will throw exception as we cannot * call remove method twice per next or previous method call */ listIterator.remove(); } } |
Output
1 2 |
Exception in thread "main" java.lang.IllegalStateException at java.util.ArrayList$Itr.remove(Unknown Source) |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
List<String> listWeekDays = new ArrayList<String>(); listWeekDays.add("Monday"); listWeekDays.add("Tuesday"); listWeekDays.add("Wednesday"); listWeekDays.add("Thursday"); listWeekDays.add("Friday"); ListIterator<String> listIterator = listWeekDays.listIterator(); String strDay = null; while(listIterator.hasNext()){ strDay = listIterator.next(); if(strDay.equals("Tuesday")){ System.out.println("previous method will return the element at index: " + listIterator.previousIndex()); System.out.println("next method will return the element at index: " + listIterator.nextIndex()); } } |
Output
1 2 |
previous method will return element at index: 1 next method will return element at index: 2 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
List<String> listWeekDays = new ArrayList<String>(); listWeekDays.add("Monday"); listWeekDays.add("Tuesday"); listWeekDays.add("Wednesday"); listWeekDays.add("Thursday"); listWeekDays.add("Friday"); ListIterator<String> listIterator = listWeekDays.listIterator(); String strDay = null; while(listIterator.hasNext()){ strDay = listIterator.next(); if(strDay.equals("Wednesday")){ //replace Wednesday with Sunday listIterator.set("Sunday"); } } System.out.println("List contains: " + listWeekDays); |
Output
1 |
List contains: [Monday, Tuesday, Sunday, Thursday, Friday] |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
List<String> listWeekDays = new ArrayList<String>(); listWeekDays.add("Monday"); listWeekDays.add("Tuesday"); listWeekDays.add("Wednesday"); listWeekDays.add("Thursday"); listWeekDays.add("Friday"); ListIterator<String> listIterator = listWeekDays.listIterator(); //this will insert Sunday at the start of the list listIterator.add("Sunday"); String strDay = null; while(listIterator.hasNext()){ strDay = listIterator.next(); if(strDay.equals("Friday")){ //this will add Saturday after Friday listIterator.add("Saturday"); } } System.out.println("List contains: " + listWeekDays); |
Output
1 |
List contains: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday] |
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
- How to iterate ArrayList using ListIterator
- ArrayList ListIterator example
- How to iterate through List using ListIterator
- How to iterate ArrayList in reverse direction using ListIterator
- Iterate LinkedList in reverse direction using the ListIterator
- How to iterate LinkedHashMap using an Iterator
- How to iterate LinkedHashMap in reverse order using ListIterator
- How to iterate Vector using the ListIterator
- How to iterate through Vector in reverse direction using the ListIterator
References:
Java ListIterator interface Javadoc
Please let me know if you liked the Java ListIterator tutorial with examples in the comments section below.