So far, all our functions were short one-liners. Here is another example:
Supplier<Integer> input = () -> 3;
Predicate<Integer> checkValue = d -> d < 5;
Function<Integer, Double> calculate = i -> i * 5.0;
Consumer<Double> printResult = d -> System.out.println("Result: " + d);
if(checkValue.test(input.get())){
printResult.accept(calculate.apply(input.get()));
} else {
System.out.println("Input " + input.get() + " is too small.");
}
If the function consists of two or more lines, we could implement them as follows:
Supplier<Integer> input = () -> {
// as many line of code here as necessary
return 3;
};
Predicate<Integer> checkValue = d -> {
// as many line of code here as necessary
return d < 5;
};
Function<Integer, Double> calculate = i -> {
// as many...