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.
Can you use the same variable name in separate lambda expressions?
Yes, variable names used within separate lambda expressions do not conflict, as the variable names go out of scope after the expression is evaluated.
What are the three parts of the syntax for creating a lambda expression in Java?
The syntax for a lambda expression consists of a parameter list, an arrow token (->), and a body.
Provide an example of a lambda expression using a custom Predicate.
Predicate<String> containsLetterA = str -> str.contains("a");
How is the type of a lambda expression determined in Java?
The type of a lambda expression is determined from the context in which it is used. The compiler recognizes the lambda’s type based on how it’s assigned to a variable or used in a method parameter.
What is the syntax of a lambda predicate expression?
The syntax of a lambda predicate expression is (parameter_list) -> { lambda_body }.
How is the test() method used in the Predicate interface?
The test() method is the functional method of the Predicate interface. It accepts an argument of a specified type and returns a boolean value, indicating whether the condition specified by the predicate is met or not.
What can be omitted in a simple lambda predicate expression that returns a boolean?
In a simple lambda predicate expression that returns a boolean, we can omit:
* the body curly braces
* the return keyword
* the semicolon closing the statement within the body
What is the purpose of the Predicate interface?
The Predicate interface in the java.util.function package contains one abstract method, test(T t), which takes an input object and returns a boolean value.
Provide an example of implementing the Predicate interface.
public class Student21PlusPredicate implements Predicate<Student> { @Override public boolean test(Student s) { return s.getAge() >= 21; } }