I'm reading oracle tutorial on lambda expressions: https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
To specify the search criteria, you implement the CheckPerson interface:
interface CheckPerson {
boolean test(Person p); }
then use it
printPersons(
roster,
new CheckPerson() {
public boolean test(Person p) {
return p.getGender() == Person.Sex.MALE
&& p.getAge() >= 18
&& p.getAge() <= 25;
}
}
);
then
The CheckPerson interface is a functional interface. A functional interface is any interface that contains only one abstract method. (A functional interface may contain one or more default methods or static methods.) Because a functional interface contains only one abstract method, you can omit the name of that method when you implement it. To do this, instead of using an anonymous class expression, you use a lambda expression, which is highlighted in the following method invocation:
printPersons(
roster,
(Person p) -> p.getGender() == Person.Sex.MALE
&& p.getAge() >= 18
&& p.getAge() <= 25
);
They say they omit the method, I see no test
in lambda - that is clear. But they also dropped name of interface CheckPerson
. Why is it not mentioned in explanation? Do we use the CheckPerson
interface in lambda or not?
ADDED on 2019/08/29:
Thank you Alexey Soshin, Andrew Tobilko, Andreas (in the time order of answering), for your answers! I see your answers as complimenting each other to give full picture and therefore I cannot choose only one as accepted.