Back to questions
JuniorTheoryCommon
What does the program print?
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
Function<Integer, Integer> f = x -> x + 1;
Function<Integer, Integer> g = x -> x * 2;
System.out.println(f.andThen(g).apply(3));
}
}8
- Expecting
7:f.andThen(g)reads left to right, firstfand thengon its result, so 3 becomes 4 and then 8. - Expecting
4or6: the two functions form a chain,greceives the valuefreturned, andapplygives back the result ofg.
- What does
f.compose(g).apply(3)print for the samefandg? - Why is
Functionstill a functional interface although it has anandThenmethod?