Based on The Java™ Tutorials: Lambda Expressions
Reconsider the CheckPerson interface:
public interface CheckPerson {
boolean test(Person p);
}
This is a very simple interface. It's a functional interface because it contains only one abstract method. This method takes one parameter and returns a boolean value. The method is so simple that it might not be worth it to define one in your application. Consequently, the JDK defines several standard functional interfaces, which you can find in the package java.util.function.
For example, you can use the Predicate interface in place of CheckPerson. This interface contains the method boolean test(T t):
public interface Predicate<T> {
boolean test(T t);
}
The interface Predicate is an example of a generic interface. (For more information about generics, see the Generics (Updated) lesson.) Generic types (such as generic interfaces) specify one or more type parameters within angle brackets (<>). This interface contains only one type parameter, T. When you declare or instantiate a generic type with actual type arguments, you have a parameterized type. For example, the parameterized type Predicate is the following:
public interface Predicate<Person> {
boolean test(Person t);
}
This parameterized type contains a method that has the same return type and parameters as CheckPerson.boolean test(Person p). Consequently, you can use Predicate in place of CheckPerson as the following method demonstrates:
public void printPersonsWithPredicate(List<Person> roster, Predicate<Person> tester) {
for (Person p : roster) {
if (tester.test(p)) {
p.printPerson();
}
}
}
As a result, the following method invocation is the same as when you invoked printPersons in Approach 3: Specify Search Criteria Code in a Local Class to obtain members who are eligible for Selective Service:
printPersonsWithPredicate(
roster,
p -> p.getGender() == Person.Sex.MALE
&& p.getAge() >= 18
&& p.getAge() <= 25
);
This is not the only possible place in this method to use a lambda expression. The following approach suggests other ways to use lambda expressions.