- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Syntax
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.
Example
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) { System.out.println(student.name); } } private static class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } } }
Output
The final list is: Raja Adithya
- Related Articles
- How to use BooleanSupplier in lambda expression in Java?
- How to use IntSupplier in lambda expression in Java?
- How to use Supplier interface in lambda expression in Java?
- How to use BinaryOperator interface in lambda expression in Java?
- How to use UnaryOperator interface in lambda expression in Java?
- Java Program to Iterate over ArrayList using Lambda Expression
- How to use Predicate and BiPredicate in lambda expression in Java?
- How to use FileFilter interface in lambda expression in Java?\n
- How to use a return statement in lambda expression in Java?
- How to use Consumer and BiConsumer interfaces in lambda expression in Java?
- How to use Function and BiFunction interfaces in lambda expression in Java?
- How to initialize an array using lambda expression in Java?
- How to use this and super keywords with lambda expression in Java?
- How to handle an exception using lambda expression in Java?\n
- How to write a conditional expression in lambda expression in Java?

Advertisements