Join our newsletter for the latest updates.
Java Program to Iterate over ArrayList using Lambda Expression

Java Program to Iterate over ArrayList using Lambda Expression

In this example, we will learn to iterate over each elements of the arraylist using lambda expression in Java.

To understand this example, you should have the knowledge of the following Java programming topics:


Example: Pass ArrayList as Function Parameter

import java.util.ArrayList;

class Main {
  public static void main(String[] args) {
    // create an ArrayList
    ArrayList<String> languages = new ArrayList<>();

    // add elements to the ArrayList
    languages.add("Java");
    languages.add("Python");
    languages.add("JavaScript");

    // print arraylist
    System.out.print("ArrayList: ");

    // iterate over each element of arraylist
    // using forEach() method
    languages.forEach((e) -> {
      System.out.print(e + ", ");
    });
  }
}

Output

ArrayList: Java, Python, JavaScript,

In the above example, we have created an arraylist named languages. Notice the code,

languages.forEach((e) -> {
  System.out.print(e + ", ");
});

Here, we are passing the lambda expression as an argument to ArrayList forEach().