LinkedList in java is basically a part of the collection framework present in java.util package. It is the implementation of the LinkedList data structure that stores elements in a non-contiguous manner inside the memory.
Five ways to iterate a LinkedList are:
- Using for loop
- Using while loop
- Using enhanced for loop
- Using Iterator
- Using forEach() method
Method 1: Using For Loop
Java
import java.util.LinkedList;
public class GFG {
public static void main(String[] args)
{
LinkedList<Integer> linkedList = new LinkedList<>();
linkedList.add( 40 );
linkedList.add( 44 );
linkedList.add( 80 );
linkedList.add( 9 );
iterateUsingForLoop(linkedList);
}
public static void
iterateUsingForLoop(LinkedList<Integer> linkedList)
{
System.out.print(
"Iterating the LinkedList using a simple for loop : " );
for ( int i = 0 ; i < linkedList.size(); i++) {
System.out.print(linkedList.get(i) + " " );
}
}
}
|
OutputIterating the LinkedList using a simple for loop : 40 44 80 9
Method 2: Using while Loop
We can iterate our LinkedList using a while loop in a very similar way as we did using a for loop.
Java
import java.util.LinkedList;
public class GFG {
public static void main(String[] args) {
LinkedList<Character> vowels = new LinkedList<>();
vowels.add( 'a' );
vowels.add( 'e' );
vowels.add( 'i' );
vowels.add( 'o' );
vowels.add( 'u' );
iterateUsingWhileLoop(vowels);
}
public static void iterateUsingWhileLoop(LinkedList<Character> vowels){
System.out.print( "Iterating the LinkedList using while loop : " );
int i= 0 ;
while (i<vowels.size()){
System.out.print(vowels.get(i) + " " );
i++;
}
}
}
|
OutputIterating the LinkedList using while loop : a e i o u
Method 3: Using enhanced for loop
Using enhanced for loop we can sequentially iterate a LinkedList. The execution of the enhanced for loop ends after we visit all the elements. Let us see an example of iterating the LinkedList using the enhanced for loop.
Java
import java.util.LinkedList;
public class GFG {
public static void main(String[] args)
{
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add( "Geeks" );
linkedList.add( "for" );
linkedList.add( "Geeks" );
iterateUsingEnhancedForLoop(linkedList);
}
public static void iterateUsingEnhancedForLoop(LinkedList<String> linkedList)
{
System.out.print(
"Iterating the LinkedList using enhanced for loop : " );
for (String listElement : linkedList) {
System.out.print(listElement + " " );
}
}
}
|
OutputIterating the LinkedList using enhanced for loop : Geeks for Geeks
Method 4: Using Iterator
- To iterate the LinkedList using the iterator we first create an iterator to the current list and keep on printing the next element using the next() method until the next element exists inside the LinkedList.
- We check if the LinkedList contains the next element using the hasNext() method.
Java
import java.util.Iterator;
import java.util.LinkedList;
public class GFG {
public static void main(String[] args) {
LinkedList<Integer> linkedList = new LinkedList<>();
linkedList.add( 5 );
linkedList.add( 100 );
linkedList.add( 41 );
linkedList.add( 40 );
linkedList.add( 7 );
iterateUsingIterator(linkedList);
}
public static void iterateUsingIterator(LinkedList<Integer> linkedList){
System.out.print( "Iterating the LinkedList using Iterator : " );
Iterator it = linkedList.iterator();
while (it.hasNext()){
System.out.print(it.next() + " " );
}
}
}
|
OutputIterating the LinkedList using Iterator : 5 100 41 40 7
Method 5: Using forEach() method
- forEach() method was introduced in Java8.
- It performs the specified task for each element of the Iterable until all elements have been processed or the action throws an exception.
Syntax:
public void forEach(Consumer<? super E> action)
Java
import java.util.LinkedList;
public class GFG {
public static void main(String[] args)
{
LinkedList<Integer> linkedList = new LinkedList<>();
linkedList.add( 1 );
linkedList.add( 2 );
linkedList.add( 3 );
linkedList.add( 4 );
linkedList.add( 5 );
iterateUsingForEach(linkedList);
}
public static void
iterateUsingForEach(LinkedList<Integer> linkedList)
{
System.out.print(
"Iterating the LinkedList using for each function : " );
linkedList.forEach(
(element) -> System.out.print(element + " " ));
}
}
|
OutputIterating the LinkedList using for each function : 1 2 3 4 5