Method reference type and lambda in Java 8

I am wondering why method references and lambdas are not recognized as a function. Why do I need to write

Function<Integer, Integer> fun1 = i -> i+2;
Function<Integer, Integer> fun2 = i -> i*i;
fun1.compose(fun2).apply(4);

instead

((Integer i) -> i*2).compose((Integer i) -> i+2).apply(4)
+4
source share
1 answer

Lambda expressions do not have a built-in type; following error message:

Object lambda = x -> x;

Lambda expressions are poly expressions that are expressions whose type depends on their context. In particular, a lambda expression derives its type from its target type, which should be a functional interface - an interface with a single (non Object) abstract method. The same lambda expression can have several types, depending on its target type:

Predicate<String> isEmpty = s -> s.isEmpty();
Function<String, Boolean> isEmpty = s -> s.isEmpty();

Function ; , Runnable Predicate Comparable. , , Function, - .

, Function; . Lambdas ( refs) .

+12

Source: https://habr.com/ru/post/1656737/


All Articles