Iterate through List in Java

08 Feb, 2022
Lists in java allow us to maintain an ordered collection of objects. Duplicate elements as well as null elements can also be stored in a List in Java. The List interface is a part of java.util package and it inherits the Collection interface. It preserves the order of insertion.
There are several ways to iterate over List in Java. They are discussed below:
Methods:
Using loops (Naive Approach)For loopFor-each loopWhile loopUsing IteratorUsing List iteratorUsing lambda expressionUsing stream.forEach()
Using loops (Naive Approach)For loopFor-each loopWhile loop
For loop
For-each loop
While loop
Using Iterator
Using List iterator
Using lambda expression
Using stream.forEach()
Method 1-A: Simple for loop
Each element can be accessed by iteration using a simple for loop. The index can be accessed using the index as a loop variable.
Syntax:
for (i = 0; i < list_name.size(); i++) 
{
  // code block to be executed
}
Example
Java
// Java Program to iterate over List// Using simple for loop // Importing all classes of// java.util packageimport java.util.*; // CLassclass GFG {     // Main driver method    public static void main(String args[])    {        // Creating a ArrayList        List<String> myList = new ArrayList<String>();         // Adding elements to the list        // Custom inputs        myList.add("A");        myList.add("B");        myList.add("C");        myList.add("D");         // For loop for iterating over the List        for (int i = 0; i < myList.size(); i++) {             // Print all elements of List            System.out.println(myList.get(i));        }    }}
A
B
C
D
Method 1-B: Enhanced for loop
Each element can be accessed by iteration using an enhanced for loop. This loop was introduced in J2SE 5.0. It is an alternative approach to traverse the for a loop. It makes the code more readable.
Syntax:
for(data_type variable : List_name)
{  
 // Body of the loop. 
 // Each element can be accessed using variable.  
}
Java
// Java Program to Iterate over a List// using enhanced for loop (for-each) // Importing all classes of// java.util packageimport java.util.*; // Classclass GFG {     // Main driver method    public static void main(String args[])    {        // Creating an Arraylist        List<String> myList = new ArrayList<String>();         // Adding elements to the List        // Custom inputs        myList.add("A");        myList.add("B");        myList.add("C");        myList.add("D");         // Using enhanced for loop(for-each) for iteration        for (String i : myList) {             // Print all elements of ArrayList            System.out.println(i);        }    }}
A
B
C
D
Method 1-C: Using a while loop
Iterating over a list can also be achieved using a while loop. The block of code inside the loop executes until the condition is true. A loop variable can be used as an index to access each element.
Syntax:
while(variable<list_name.size())
{
   // Block of code to be executed 
}
Java
// Java Program to iterate over a List// using while loop // Importing all classes of// java.util packageimport java.util.*; // Classclass GFG {     // Main driver method    public static void main(String args[])    {        // Creating an ArrayList        List<String> myList = new ArrayList<String>();         // Adding elements to the List        // Custom inputs        myList.add("A");        myList.add("B");        myList.add("C");        myList.add("D");         // Initializing any variable to 0        int i = 0;         // If variable value is lesser than        // value indicating size of List        while (i < myList.size()) {             // Print element of list            System.out.println(myList.get(i));             // Increase variable count by 1            i++;        }    }}
A
B
C
D
Method 2: Using iterator
An iterator is an object in Java that allows iterating over elements of a collection. Each element in the list can be accessed using iterator with a while loop.
Syntax:
Iterator<data_type> variable = list_name.iterator();
Example
Java
// Java Program to iterate over the list// using iterator // Importing all classes of// java.util packageimport java.util.*; // Classclass GFG {     // Main driver method    public static void main(String args[])    {        // Creating an ArrayList        List<String> myList = new ArrayList<String>();         // Adding elements to the List        // Custom inputs        myList.add("A");        myList.add("B");        myList.add("C");        myList.add("D");         // Iterator        Iterator<String> it = myList.iterator();         // Condition check for elements in List        // using hasNext() method returning true till        // there i single element in a List        while (it.hasNext()) {             // Print all elements of List            System.out.println(it.next());        }    }}
A
B
C
D
Method 3: Using List iterator
ListIterator is an iterator is a java which is available since the 1.2 version. It allows us to iterate elements one-by-one from a List implemented object. It is used to iterator over a list using while loop.
Syntax
ListIterator<data_type> variable = list_name.listIterator();
Java
// Java program to iterate over a list// using ListIteratorimport java.util.*; // Classclass GFG {     // Main driver method    public static void main(String args[])    {        // Creating an ArrayList        List<String> myList = new ArrayList<String>();         // Adding elements to the List        // Custom inputs        myList.add("A");        myList.add("B");        myList.add("C");        myList.add("D");         // List iterator        ListIterator<String> it = myList.listIterator();         // Condition check whether there is element in List        // using hasNext() which holds true till        // there is single element in List        while (it.hasNext()) {             // Print all elements of List            System.out.println(it.next());        }    }}
A
B
C
D
Method 4: Using Iterable.forEach()
This feature is available since Java 8. It can also be used to iterate over a List. Iteration can be done using a lambda expression.
Syntax:
list_name.forEach(variable->{//block of code})
Java
// Java Program to iterate over a List// using forEach() // Importing all classes of// java.util methodimport java.util.*; // Classclass GFG {     // Main driver method    public static void main(String args[])    {        // Creating an ArrayList        List<String> myList = new ArrayList<String>();         // Adding elements to the List        // Custom inputs        myList.add("A");        myList.add("B");        myList.add("C");        myList.add("D");         // Lambda expression printing all elements in a List        myList.forEach(            (temp) -> { System.out.println(temp); });    }}
A
B
C
D
Method 5: Using Stream.forEach()
The processing order of stream().forEach() is undefined while in case of forEach(), it is defined. Both can be used to iterate over a List.
Syntax:
list_name.stream.forEach(variable->{//block of code})
Java
// Java Program iterating over a List// using stream.forEach() method // Importing all classes of// java.util methodimport java.util.*; // Classclass GFG {     // Main driver method    public static void main(String args[])    {        // Creating an ArrayList        List<String> myList = new ArrayList<String>();         // Adding elements to the List        // Custom inputs        myList.add("A");        myList.add("B");        myList.add("C");        myList.add("D");         // stream.forEach() method prints        // all elements inside a List        myList.stream().forEach(            (temp) -> System.out.println(temp));    }}
A
B
C
D
surinderdawra388
singghakshay
rkbhola5
Java-Collections
java-list
Picked
Java
Java Programs
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.