Java Program to Iterate over ArrayList using Lambda Expression

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().

Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community