Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How lambda expression works with Predicate?

I need more clarification about lambda expression. How 'p' represents List<Person> people? Could you explain clear to me

List<Person> people = new ArrayList<>();
people.add(new Person("Mohamed", 69));
people.add(new Person("Doaa", 25));
people.add(new Person("Malik", 6));
Predicate<Person> pred = (p) -> p.getAge() > 65;
like image 633
Kannan Thangadurai Avatar asked Feb 08 '23 14:02

Kannan Thangadurai


1 Answers

No, p is not a List<Person> but a Person.

Predicate<Person> pred = p -> p.getAge() > 65;

This lambda expression declares 1 formal parameter and returns a boolean. As such, it can be represented as a Predicate (because the Predicate interface has a single functional method with this exact signature, called test). The type of p will be determined by the type of the Predicate you are creating.

For example, in the following code, p will be a List<Person>:

Predicate<List<Person>> predicate = p -> p.isEmpty();
like image 105
Tunaki Avatar answered Feb 16 '23 03:02

Tunaki