Topic 9.5: Working with Selected classes from the Java API - Write a simple Lambda expression that consumes a Lambda Predicate expression Flashcards
How do we use a lambda predicate expression to check if a student’s age is greater than or equal to 21?
(student) -> { return student.getAge() >= 21; }.
Provide an example of using a Predicate object in a method.
public static List<Student> getMatching(List<Student> students, Predicate<Student> ps) { List<Student> results = new ArrayList<>(); for (Student s : students) { if (ps.test(s)) { results.add(s); } } return results; }
What is the purpose of the test() method in the Predicate interface?
The test() method in the Predicate interface evaluates the condition specified by the predicate on the given input object and returns true or false accordingly.
How are lambda expressions related to functional interfaces?
Lambda expressions allow you to create instances of functional interfaces concisely and inline, without the need for explicit class implementations.
What is the functionality of the or() method in the Predicate interface?
The or() method returns a composed predicate that represents a logical OR operation between the current predicate and another predicate. It returns true if either of the predicates evaluates to true.
Show a code snippet that uses the negate() method in the Predicate interface to create a negated predicate.
Predicate<Integer> isEven = num -> num % 2 == 0; Predicate<Integer> isOdd = isEven.negate(); boolean result = isOdd.test(3); // true
What is the purpose of functional interfaces in Java?
Functional interfaces serve as the foundation for lambda expressions and provide a way to represent functions as objects in Java.
What are the options for representing the implementation of a lambda expression in Java?
The body of a lambda expression can be a single expression/statement or a block of code enclosed in curly braces {}.
- If it’s a single expression, it is implicitly returned.
- If it’s a block of code and a value is expected, you need to include a return statement explicitly.
What is the purpose of the test() method in the Predicate interface?
The test() method in the Predicate interface is used to evaluate a condition on the input argument and returns a boolean value (true or false).
What does the negate() method do in the Predicate interface?
The negate() method returns a predicate that represents the negation of the current predicate. It flips the result of the current predicate; if the current predicate returns true, the negated predicate will return false, and vice versa.
Can you specify argument types (formal parameter types) in lambda expressions?
Yes, you can specify argument types in lambda expressions. However, if you provide the argument type for one parameter, you must provide it for all parameters when the context requires multiple parameters. This helps remove ambiguity for the compiler.
How do you implement the Predicate interface?
To implement the Predicate interface, use the syntax: class ClassName implements Predicate<T>, where T is the type of the input to the predicate.</T>
What is the benefit of using a Predicate object?
By using a Predicate object, we can encapsulate logic that will return a boolean value. this logic may then be re-used and passed to a generalized method, making the method dynamic and reusable without the need for rewriting the logic.
What does the Predicate functional interface represent in Java?
The Predicate functional interface represents a boolean-valued function that takes an input and returns true or false based on a specified condition.
What is the general rule for creating lambda expressions in Java?
Lambda expressions can only be used with interfaces that have exactly one abstract method. These interfaces are known as functional interfaces.