In this tutorial, we are going to learn how to Iterate an ArrayList using ‘ListIterator’ in Java with example(s).The listIterator() method is part of java.util class (specifically from java.util.ArrayList). It allows to traverse the list in either direction forward or backward, modify the list during iteration, and obtain the iterator’s current position in the list.
Look at ListIterator Javadoc description below:
Check out: Unique ways to Filter an ArrayList in Java
ListIterator in Java with Examples...!!! Click To Tweet
Example 1. Iterate through the List in the ‘Forward / Backward Direction’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | /** * Iterate through a List in forward and backward direction with ListIterator * @author Deepak Verma */ import java.util.Arrays; import java.util.List; import java.util.ListIterator; public class ListIterator_Forward_Backward { public static void main(String args[]) { //Create a List and Add 5 Items to the List List<String> websiteList = Arrays.asList("Baeldung.com", "Techndeck.com", "W3Schools.com", "Mkyong.com", "Stackoverflow.com"); //Get ListIterator ListIterator<String> websiteListIterator = websiteList.listIterator(); //Traverse the List in the forward direction System.out.println("----- Traverse the List in the Forward Direction -----"); while (websiteListIterator.hasNext()) { System.out.println(websiteListIterator.next()); } System.out.println(" \n----- Traverse the List in the Backward Direction -----"); while (websiteListIterator.hasPrevious()) { System.out.println(websiteListIterator.previous()); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | ----- Traverse the List in the Forward Direction ----- Baeldung.com Techndeck.com W3Schools.com Mkyong.com Stackoverflow.com ----- Traverse the List in the Backward Direction ----- Stackoverflow.com Mkyong.com W3Schools.com Techndeck.com Baeldung.com |
Example 2. Iterate through the List and start from a ‘Specified Position’ using Index
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | /** * Iterate a List from a specified position or index with ListIterator * @author Deepak Verma */ import java.util.Arrays; import java.util.List; import java.util.ListIterator; public class ListIterator_Index { public static void main(String args[]) { //Create a List and Add 5 Items to the List List<String> websiteList = Arrays.asList("Baeldung.com", "Techndeck.com", "W3Schools.com", "Mkyong.com", "Stackoverflow.com"); //Get ListIterator from Index 3 ListIterator<String> websiteListIterator = websiteList.listIterator(3); //Traverse the List in the forward direction System.out.println("----- Traverse the List from Index 3 meaning Position 4 -----"); while (websiteListIterator.hasNext()) { System.out.println(websiteListIterator.next()); } } } |
1 2 3 | ----- Traverse the List from Index 3 meaning Position 4 ----- Mkyong.com Stackoverflow.com |
In Java Programming, Iterator and ListIterator both are very commonly used ways to iterate through an ArrayList. ListIterator has no many methods other than hasNext(), hasPrevious() and Index like add, nextIndex, previousIndex, remove and set. All these methods are commonly used as well based on the coding requirement. At Techndeck, will try to create more tutorials in the future to explain them as well. But for now, go ahead and give them a try. I hope above example(s) could help you to get a better understanding on ListIterator in Java.
ListIterator in Java with Examples...!!! Click To Tweet
Do you like this Post? – then check my other helpful posts:
- Passing Function as a Parameter in another Method in Java 8
- Collection sorting using Lambda in Java 8
- Generate Prime numbers in Java 8
- Java 8 program to calculate average of N numbers
- Reverse a word in a string in Java 8
- Finding prime number using Java 8
- Double the numbers of specified ArrayList using Streams
Other Useful References: