Topic 9.5: Working with Selected classes from the Java API - Write a simple Lambda expression that consumes a Lambda Predicate expression Flashcards

1
Q

How do we use a lambda predicate expression to check if a student’s age is greater than or equal to 21?

A

(student) -> { return student.getAge() >= 21; }.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Provide an example of using a Predicate object in a method.

A
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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the purpose of the test() method in the Predicate interface?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How are lambda expressions related to functional interfaces?

A

Lambda expressions allow you to create instances of functional interfaces concisely and inline, without the need for explicit class implementations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the functionality of the or() method in the Predicate interface?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Show a code snippet that uses the negate() method in the Predicate interface to create a negated predicate.

A
Predicate<Integer> isEven = num -> num % 2 == 0;
Predicate<Integer> isOdd = isEven.negate();
boolean result = isOdd.test(3); // true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the purpose of functional interfaces in Java?

A

Functional interfaces serve as the foundation for lambda expressions and provide a way to represent functions as objects in Java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the options for representing the implementation of a lambda expression in Java?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the purpose of the test() method in the Predicate interface?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the negate() method do in the Predicate interface?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can you specify argument types (formal parameter types) in lambda expressions?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you implement the Predicate interface?

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the benefit of using a Predicate object?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the Predicate functional interface represent in Java?

A

The Predicate functional interface represents a boolean-valued function that takes an input and returns true or false based on a specified condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the general rule for creating lambda expressions in Java?

A

Lambda expressions can only be used with interfaces that have exactly one abstract method. These interfaces are known as functional interfaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Can you use the same variable name in separate lambda expressions?

A

Yes, variable names used within separate lambda expressions do not conflict, as the variable names go out of scope after the expression is evaluated.

17
Q

What are the three parts of the syntax for creating a lambda expression in Java?

A

The syntax for a lambda expression consists of a parameter list, an arrow token (->), and a body.

18
Q

Provide an example of a lambda expression using a custom Predicate.

A

Predicate<String> containsLetterA = str -> str.contains("a");

19
Q

How is the type of a lambda expression determined in Java?

A

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.

20
Q

What is the syntax of a lambda predicate expression?

A

The syntax of a lambda predicate expression is (parameter_list) -> { lambda_body }.

21
Q

How is the test() method used in the Predicate interface?

A

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.

22
Q

What can be omitted in a simple lambda predicate expression that returns a boolean?

A

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

23
Q

What is the purpose of the Predicate interface?

A

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.

24
Q

Provide an example of implementing the Predicate interface.

A
public class Student21PlusPredicate implements Predicate<Student> 
{
    @Override
    public boolean test(Student s) {
        return s.getAge() >= 21;
    }
}
25
Q

What is the scope of lambda expression parameters?

A

The parameter names used in lambda expressions should not conflict with the variable names currently in scope.

The variable names used in lambda expressions do not escape the lambda’s scope.

26
Q

Show a code snippet that uses the and() method in the Predicate interface to combine two predicates.

A
Predicate<Integer> isEven = num -> num % 2 == 0;
Predicate<Integer> isGreaterThanTen = num -> num > 10;

Predicate<Integer> isEvenAndGreaterThanTen = isEven.and(isGreaterThanTen);
boolean result = isEvenAndGreaterThanTen.test(12); // true
27
Q

When can we omit the parentheses around the parameter in a lambda predicate expression?

A

If we are only passing a single parameter to the lambda body, we may omit the parentheses around it.

For example: s -> s.getAge() > 18

28
Q

How does the and() method of the Predicate interface work?

A

The and() method returns a composed predicate that represents a logical AND operation between the current predicate and another predicate. It returns true only if both predicates evaluate to true.

29
Q

Provide a code snippet demonstrating the use of the or() method in the Predicate interface.

A
Predicate<Integer> isEven = num -> num % 2 == 0;
Predicate<Integer> isDivisibleByThree = num -> num % 3 == 0;

Predicate<Integer> isEvenOrDivisibleByThree = isEven.or(isDivisibleByThree);
boolean result = isEvenOrDivisibleByThree.test(12); // true
30
Q

What happens when you use a lambda expression to implement the functional method of a functional interface?

A

When you use a lambda expression to implement the functional method of a functional interface, you effectively create an instance of that interface.

31
Q

What characterizes a functional interface in Java?

A

A functional interface contains only a single abstract method, which is often referred to as the functional method.

32
Q

How do lambda expressions simplify implementing the Predicate interface?

A

Lambda expressions provide a shorthand way to implement the test() method of the Predicate interface without the need to create a separate implementing class.

33
Q

Where is the Predicate interface located, and what does it belong to?

A

The Predicate interface is located in the java.util.function package, and it belongs to the group of functional interfaces in Java.

34
Q

What are lambda expressions in Java?

A

Lambda expressions in Java are anonymous functions that provide a concise way to represent functional interfaces with a single abstract method.

35
Q

How do you create a lambda expression using Predicate to evaluate a custom condition?

A

To create a lambda expression using Predicate to evaluate a custom condition, you need to implement the test() method within the lambda’s body.

36
Q

How does the relationship between functional interfaces and lambda expressions impact Java code?

A

The relationship allows for the creation of compact and expressive code, making Java more functional in nature. By using lambda expressions with functional interfaces, you can pass behavior as a method argument, store it in variables, or return it from methods.

37
Q

Provide a code snippet demonstrating the use of the test() method in the Predicate interface.

A
Predicate<String> isLongWord = word -> word.length() > 5;
boolean result = isLongWord.test("Java"); // false
38
Q

What is a Predicate in Java?

A

This is a statement or function in Java that returns either true or false.