In this article, we are going to see how to iterate through a List. In Java, a List is an interface of the Collection framework. List can be of various types such as ArrayList, Stack, LinkedList, and Vector. There are various ways to iterate through a java List but here we will only be discussing our traversal using loops only. So, there were standard three traversals available so do three methods do exists but with the introduction of java 8 and streams other methods do arises out. So, all the four methods are discussed below as follows:
Methods:Â
- For loop Method
- While Method
- For-each loop Method
- For-each loop of java 8
Implementation:
Method 1: Using a for loop
 For Loop is the most common flow control loop. For loop uses a variable to iterate through the list.Â
ExampleÂ
Java
// Java Program to Iterate List in java // using for loop // Importing all input output classes import java.io.*; // Importing all utility classes from // java.util package import java.util.*; Â
// Class class GFG { Â
    // main driver method     public static void main(String[] args)     {         // Creating an ArrayList object         // Declaring object of Integer type         // Custom entries in array         List<Integer> my_list             = Arrays.asList( 10 , 20 , 30 , 40 , 50 ); Â
        // Display message         System.out.print( "Iterating over ArrayList: " ); Â
        // Iteration over ArrayList         // using the for loop         for ( int i = 0 ; i < my_list.size(); i++) Â
            // Print and display the all elements             // in List object             System.out.print(my_list.get(i) + " " ); Â
        // new line         System.out.println(); Â
        // No, creating a vector of size N         // Custom entry for N = 5         // Custom Integer entries         List<Integer> v = new Vector<Integer>( 5 );         v.add( 10 );         v.add( 20 );         v.add( 30 );         v.add( 40 );         v.add( 50 ); Â
        // Display message         System.out.print( "Iterating over Vector:   " ); Â
        // iterating over vector using for loop         for ( int i = 0 ; i < v.size(); i++) Â
            // Print and display vector elements             System.out.print(v.get(i) + " " ); Â
        // New Line         System.out.println(); Â
        // Creating a stack containing Integer elements         List<Integer> s = new Stack<Integer>(); Â
        // Adding integer elements         // Custom input         s.add( 10 );         s.add( 20 );         s.add( 30 );         s.add( 40 );         s.add( 50 ); Â
        // Display message         System.out.print( "Iterating over Stack:    " ); Â
        // For loop o iterate over elements in stack         for ( int i = 0 ; i < v.size(); i++) Â
            // Print and display all stack elements             System.out.print(s.get(i) + " " );     } } |
Â
Â
Iterating over ArrayList: 10 20 30 40 50 Iterating over Vector: 10 20 30 40 50 Iterating over Stack: 10 20 30 40 50
Â
Method 2: Using While loop
Â
Java while loop similar to For loop is a control flow statement that allows code to run repeatedly until a desired condition is met.Â
Â
ExampleÂ
Â
Java
// Java Program to iterate over List // using while loop Â
// Importing all input output classes import java.io.*; // Importing all utility classes from // java.util package import java.util.*; Â
// Class class GFG { Â
    // Main driver method     public static void main(String[] args)     {         // Creating an object of List         // Declaring object of Integer type         // Custom Integer entries         List<Integer> my_list             = Arrays.asList( 10 , 20 , 30 , 40 , 50 ); Â
        // Display message         System.out.print( "Iterating over ArrayList: " ); Â
        // Initially loop variable is initialized         // with zero         int i = 0 ; Â
        // Iterating over List via while loop         // using size() method         while (i < my_list.size()) { Â
            // Print and display all elements             // of an ArrayList             System.out.print(my_list.get(i) + " " ); Â
            // Incrementing the counter by unity safter             // one iteration             i++;         } Â
        i = 0 ; Â
        // New Line         System.out.println(); Â
        // Creating a Vector of size N         // Custom value for N = 5         List<Integer> v = new Vector<Integer>( 5 ); Â
        // Adding 5 elements to the above List object         // for vector         // Custom entries         v.add( 10 );         v.add( 20 );         v.add( 30 );         v.add( 40 );         v.add( 50 ); Â
        // Display message         System.out.print( "Iterating over Vector:   " ); Â
        // Iterating over Vector via while loop         // using the size() method         while (i < v.size()) { Â
            // Print and display all elements of vector             System.out.print(v.get(i) + " " ); Â
            // Increment the counter variable             i++;         } Â
        // Counter variable is initially         // initialized with zero         i = 0 ; Â
        // New Line         System.out.println(); Â
        // Creating a Stack by creating another         // list object of Integer type         // Declaring object of Integer type         List<Integer> s = new Stack<Integer>(); Â
        // Adding elements to the above stack         // Custom entries         s.add( 10 );         s.add( 20 );         s.add( 30 );         s.add( 40 );         s.add( 50 ); Â
        // Display message         System.out.print( "Iterating over Stack:    " ); Â
        // Iterating over stack via while loop         // using size method()         while (i < v.size()) { Â
            // Print and display all elements             // of the above stack/ obj created             System.out.print(s.get(i) + " " ); Â
            // Increment the counter by unity             i++;         }     } } |
Â
Â
Iterating over ArrayList: 10 20 30 40 50 Iterating over Vector: 10 20 30 40 50 Iterating over Stack: 10 20 30 40 50
Â
Method 3: Using for each loop
Â
Syntax:
Â
for (type temp : list_name) { statements using temp; }
Â
ExampleÂ
Â
Java
/*package whatever //do not write package name here */ Â
import java.io.*; import java.util.*; class GFG {     public static void main(String[] args)     {         // creating Arraylist         List<Integer> my_list             = Arrays.asList( 10 , 20 , 30 , 40 , 50 ); Â
              System.out.print( "Iterating over ArrayList: " );          // For Each Loop for iterating ArrayList         for (Integer i :my_list)             System.out.print(i + " " ); Â
        System.out.println(); Â
        // creating Vector of size 5         List<Integer> v = new Vector<Integer>( 5 );         v.add( 10 );         v.add( 20 );         v.add( 30 );         v.add( 40 );         v.add( 50 ); Â
        System.out.print( "Iterating over Vector:   " );          // For Each Loop for iterating Vector         for (Integer i : v)             System.out.print(i + " " ); Â
        System.out.println(); Â
        // creating Stack         List<Integer> s = new Stack<Integer>();         s.add( 10 );         s.add( 20 );         s.add( 30 );         s.add( 40 );         s.add( 50 ); Â
                System.out.print( "Iterating over Stack:    " );         // For Each Loop for iterating Stack         for (Integer i : s)             System.out.print(i + " " );     } } |
Â
Â
Iterating over ArrayList: 10 20 30 40 50 Iterating over Vector: 10 20 30 40 50 Iterating over Stack: 10 20 30 40 50
Â
Method 4: Using for each loop of Java 8
Â
This method takes a functional interface as a parameter therefore lambda expression can be passed as an argument.
Â
Syntax:
Â
void forEach(Consumer<? super T> action)
Â
ExampleÂ
Â
Java
// Importing all input output classes import java.io.*; // Importing all classes from // java,util package import java.util.*; Â
// Class class GFG { Â
    // Main driver method     public static void main(String[] args)     {         // Creating an Arraylist by creating object         // of List and declaring as Integer type         // Custom Integer entries         List<Integer> my_list             = Arrays.asList( 10 , 20 , 30 , 40 , 50 ); Â
        // Display message         System.out.print( "Iterating over ArrayList: " ); Â
        // Traversing over ArrayList         // using for each method Java 8         my_list.forEach(             list -> System.out.print(list + " " )); Â
        // New line         System.out.println(); Â
        // creating Vector by creating object of         // List and declaring as Integer type Â
        // Vector is of size N         // N = 5 for illustration purposes         List<Integer> v = new Vector<Integer>( 5 ); Â
        // Adding elements to the vector         // Custom Integer elements         v.add( 10 );         v.add( 20 );         v.add( 30 );         v.add( 40 );         v.add( 50 ); Â
        // Display message         System.out.print( "Iterating over Vector:   " ); Â
        // Traversing the above vector elements         // using for each method Java 8         v.forEach(vector -> System.out.print(vector + " " )); Â
        // New line         System.out.println(); Â
        // Creating a Stack by creating an object of         // List and declaring it as of Integer type         List<Integer> s = new Stack<Integer>(); Â
        // Adding elements to the above stack created         // Custom inputs addition using add() method         s.add( 10 );         s.add( 20 );         s.add( 30 );         s.add( 40 );         s.add( 50 ); Â
        // Display message         System.out.print( "Iterating over Stack:    " ); Â
        // Print and display all the elements inside stack         // using for each method Java 8         s.forEach(stack -> System.out.print(stack + " " ));     } } |
Â
Â
Iterating over ArrayList: 10 20 30 40 50 Iterating over Vector: 10 20 30 40 50 Iterating over Stack: 10 20 30 40 50
Â