How to use an ArrayList in lambda expression in Java?n

The lambda expression is an inline code that can implement a functional interface without creating an anonymous class. An ArrayList can be used to store a dynamically sized collection of elements.

In the below program, We have removed the elements of an ArrayList whose age is less than or equal to 20 using the removeIf() method. This method introduced in Java 8 version to remove all elements from a collection that satisfy a condition.


public boolean removeIf(Predicate filter)

The parameter filter is a Predicate. If the given predicate satisfies the condition, the element can be removed. This method returns the boolean value true if elements are removed, false otherwise.


import java.util.*;

public class LambdaWithArrayListTest {
   public static void main(String args[]) {
      ArrayList<Student> studentList = new ArrayList<Student>();
      studentList.add(new Student("Raja", 30));
      studentList.add(new Student("Adithya", 25));
      studentList.add(new Student("Jai", 20));
      studentList.removeIf(student -> (student.age <= 20)); // Lambda Expression
      System.out.println("The final list is: ");
      for(Student student : studentList) {
   private static class Student {
      private String name;
      private int age;
      public Student(String name, int age) { = name;
         this.age = age;


The final list is: