This anonymous class feature was from the day one.
printMembers(
roster,
new CheckMember() {
public boolean test(Member p) {
return p.getGender() == Member.Sex.MALE
&& p.getAge() >= 18
&& p.getAge() <= 25;
}
}
);
But with the new Lamda expression it will become like this:
printMembers(
roster,
(Member p) -> p.getGender() == Member.Sex.MALE
&& p.getAge() >= 18
&& p.getAge() <= 25
);
Here is how the magik works.
The interface "CheckMember" should have only one member method. The interface that has only one member method is called "functional interface".
interface CheckMember {
boolean test(Member p);
}
Because functional interface has only one member method, we can omit the obvious function name.
No comments:
Post a Comment