Iterating over a LinkedList in Java
This article is about understanding the process of iterate over a LinkedList Collection in Java and access objects.
So before going to the main program for iteration, let’s start with some fundamental things then we will see how we can do that.
So, to fetch the Iterator from a LinkedList object we can use
ListIterator<E> listIterator();
This will return ListIterator object which implements Iterator and which provides functions to iterate over the LinkedList object. As like a head reference in LinkedList data structure it manages an internal cursor where as this function helps in pointing to the first element if available.
To return the next element of the list and to update the internal cursor to point to next element use the below function
E next();
To check there is any next element available or not use the below function
boolean hasNext();
To return the previous element in the list and to update the internal cursor to point to previous element use the below function
E previous();
To check there is any next element available or not (in terms of previous) use the below function
boolean hasPrevious();
As above we saw how that iterator pointed to the start of LinkedList like that there is an overloaded version of listIterator()
which can return the Iterator that will point to a specified index
ListIterator<E> listIterator(int index);
Iterating over the LinkedList object in forward direction :-
// Java Program import java.util.LinkedList; import java.util.List; import java.util.ListIterator; public class Main { public static void main(String[] args) { // A LinkedList of String objects is created List<String> sampleList = new LinkedList<>(); // Elements are added at the end of LinkedList sampleList.add("100"); sampleList.add("150"); sampleList.add("200"); sampleList.add("250"); sampleList.add("300"); sampleList.add("350"); sampleList.add("400"); sampleList.add("450"); sampleList.add("500"); // Getting the ListIterator from List Object // Here it points to 0th position in List ListIterator<String> listIt = sampleList.listIterator(); System.out.println("Iterating over LinkedList in forward direction :"); // Iterating over LinkedList in forward direction while(listIt.hasNext()) { // To which element the cursor points it will return that element // As well as it will increment the cursor to point to next position String temp = listIt.next(); System.out.println(temp); } // Getting ListIterator from List Object // which points to 3nd Position in List listIt = sampleList.listIterator(5); System.out.println("Iterating List in forward Direction but starting from 5th Position :"); // Iterating over LinkedList in forward direction while(listIt.hasNext()) { String temp = listIt.next(); System.out.println(temp); } } }
Output : Iterating over LinkedList in forward direction : 100 150 200 250 300 350 400 450 500 Iterating List in forward Direction but starting from 5th Position : 350 400 450 500
Iterating over the LinkedList object in backward direction :-
// Java Program import java.util.LinkedList; import java.util.List; import java.util.ListIterator; public class Main { public static void main(String[] args) { // A LinkedList of String objects is created List<String> sampleList = new LinkedList<>(); // Elements are added at the end of LinkedList sampleList.add("100"); sampleList.add("150"); sampleList.add("200"); sampleList.add("250"); sampleList.add("300"); sampleList.add("350"); sampleList.add("400"); sampleList.add("450"); sampleList.add("500"); // Getting the ListIterator from List Object // Here it points to 0th position in List ListIterator<String> listIt = sampleList.listIterator(); System.out.println("Iterating over LinkedList in backward direction :"); // Iterating over LinkedList in backward direction // so last position is passed as 9 listIt = sampleList.listIterator(9); while(listIt.hasPrevious()) { // To which element the cursor points it will return that element // As well as it will increment the cursor to point to next position String temp = listIt.previous(); System.out.println(temp); } // Getting ListIterator from List Object // which points to 5th Position in List listIt = sampleList.listIterator(5); System.out.println("Iterating List in backward Direction but starting from 5th Position :"); // Iterating over LinkedList in backward direction while(listIt.hasPrevious()) { String temp = listIt.previous(); System.out.println(temp); } } }
Output : Iterating over LinkedList in backward direction : 500 450 400 350 300 250 200 150 100 Iterating List in backward Direction but starting from 5th Position : 300 250 200 150 100