
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Use ArrayList in Lambda Expression in Java
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
Advertisements