Java 8 Quiz
- Quiz
- 1. which of the following are not valid lambda expressions?
- 2. Which of these interfaces are functional interfaces?
- 3. Which of the following are valid uses of lambda expressions?
- 4. What functional interfaces would you use for the following function descriptors?
- 5. How could you fix the problem?
- 6. What are equivalent method references for the following lambda expressions?
- Answer
Quiz
1. which of the following are not valid lambda expressions?
- () -> {}
- () -> “Raoul”
- () -> {return “Mario”;}
- (Integer i) -> return “Alan” + i;
- (String s) -> {“Iron Man”;}
2. Which of these interfaces are functional interfaces?
public interface Adder{
int add(int a, int b);
}
public interface SmartAdder extends Adder{
int add(double a, double b);
}
public interface Nothing{
}
3. Which of the following are valid uses of lambda expressions?
- A
execute(() -> {});
public void execute(Runnable r){
r.run();
}
- B
public Callable<String> fetch() {
return () -> "Tricky example ;-)";
}
- C
Predicate<Apple> p = (Apple a) -> a.getWeight();
public interface Predicate<T> {
boolean predicate(T t);
}
4. What functional interfaces would you use for the following function descriptors?
- T -> R
- (int, int) -> int
- T -> void
- () -> T
- (T, U) -> R
5. How could you fix the problem?
Object o = () -> {System.out.println(“Tricky example”); };
6. What are equivalent method references for the following lambda expressions?
- A
Function<String, Integer> stringToInteger = (String s) -> Integer.parseInt(s);
- B
BiPredicate<List
Answer
1. Only 4 and 5 are invalid lambdas
To correct 4:
(Integer i) -> {return "Alan" + i;}
To correct 5:
(String s) -> {return "Iron Man";}
2. Only Adder is a functional interface
3. Only A and B are valid.
A is valid because the lambda () -> {} has the signature () -> void, which matches the signature of the abstract method run defined in Runnable. Note that running this code will do nothing because the body of the lambda is empty!
C is invalid because the lambda expression (Apple a) -> a.getWeight() has the signature (Apple) -> Integer, which is different than the signature of the method test defined in Predicate
4. all
- Function<T, R> is a good candidate. It’s typically used for converting an object of type T into an object of type R (for example, Function<Apple, Integer> to extract the weight of an apple).
- IntBinaryOperator has a single abstract method called applyAsInt representing a function descriptor (int, int) -> int.
- Consumer
has a single abstract method called accept representing a function descriptor T -> void. - Supplier
has a single abstract method called get representing a function descriptor () -> T. Alternatively, Callable also has a single abstract method called call representing a function descriptor () -> T. - BiFunction<T, U, R> has a single abstract method called apply representing a function descriptor (T, U) -> R.
5. answer
The context of the lambda expression is Object (the target type). But Object isn’t a functional interface. To fix this you can change the target type to Runnable, which represents a function descriptor () -> void:
Runnable r = () -> {System.out.println(“Tricky example”); };
6. answer for 6
- A
Function<String, Integer> stringToInteger = Integer::parseInt
- B
BiPredicate<List