Java 8 lambdas执行

Java 8中如何做这样的事情?

boolean x = ((boolean p)->{return p;}).apply(true);


The target type of this expression must be a functional interface

根据JLS section 15.27

It is a compile-time error if a lambda expression occurs in a program in someplace other than an assignment context (§5.2), an invocation context (§5.3), or a casting context (§5.5).

也可以在return statement中使用lambda表达式.



Function<Boolean, Boolean> function = p -> p;
boolean x = function.apply(true);


foobar(p -> p);

private static void foobar(Function<Boolean, Boolean> function) {
    boolean x = function.apply(true);


boolean x = ((Function<Boolean, Boolean>) p -> p).apply(true);


boolean x = function().apply(true);

private static Function<Boolean, Boolean> function() {
    return p -> p;


UnaryOperator<Boolean> function = UnaryOperator.identity();

转载注明原文:Java 8 lambdas执行 - 代码日志