在Java 8中,方法可以创建为Lambda表达式,并且可以通过引用传递(在底层做一些工作)。网上有很多创建lambdas并将其与方法一起使用的示例,但没有示例说明如何创建以lambda作为参数的方法。它的语法是什么?

MyClass.method((a, b) -> a+b);


class MyClass{
  //How do I define this method?
  static int method(Lambda l){
    return l(5, 10);
  }
}

当前回答

Lambda不是一个对象,而是一个功能接口。 使用@FuntionalInterface作为注释,可以定义尽可能多的功能接口

@FuntionalInterface
public interface SumLambdaExpression {
     public int do(int a, int b);
}

public class MyClass {
     public static void main(String [] args) {
          SumLambdaExpression s = (a,b)->a+b;
          lambdaArgFunction(s);
     }

     public static void lambdaArgFunction(SumLambdaExpression s) {
          System.out.println("Output : "+s.do(2,5));
     }
}

输出将如下所示

Output : 7

Lambda表达式的基本概念是定义自己的逻辑,但已经定义了参数。因此,在上面的代码中,您可以将do函数的定义从加法更改为任何其他定义,但参数限制为2。

其他回答

您可以使用如上所述的功能接口。 下面是一些例子

Function<Integer, Integer> f1 = num->(num*2+1);
System.out.println(f1.apply(10));

Predicate<Integer> f2= num->(num > 10);
System.out.println(f2.test(10));
System.out.println(f2.test(11));

Supplier<Integer> f3= ()-> 100;
System.out.println(f3.get());

希望能有所帮助

对我来说,最有意义的解决方案是定义一个Callback接口:

interface Callback {
    void call();
}

然后在你想调用的函数中使用它作为参数:

void somewhereInYourCode() {
    method(() -> {
        // You've passed a lambda!
        // method() is done, do whatever you want here.
    });
}

void method(Callback callback) {
    // Do what you have to do
    // ...

    // Don't forget to notify the caller once you're done
    callback.call();
}

只是为了精确起见

lambda不是一个特殊的接口、类或任何你可以自己声明的东西。Lambda只是()->{}特殊语法的名称,它允许在作为参数传递单方法接口时具有更好的可读性。它的设计是为了取代这个:

method(new Callback() {
    @Override
    public void call() {
        // Classic interface implementation, lot of useless boilerplate code.
        // method() is done, do whatever you want here.
    }
});

所以在上面的例子中,Callback不是一个lambda,它只是一个常规接口;Lambda是可用于实现它的快捷语法的名称。

如果你同时使用Java和Kotlin语言(就像我们在Android开发中所做的那样),你可以传递lambda函数而不定义任何额外的接口:

import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.functions.Function2;

void foo(Function0<Boolean> param1) //for zero param
{
    param1.invoke();
}

void foo1(Function1<Integer, Boolean> param1) //for one param
{
    param1.invoke(1);
}

void foo2(Function2<Integer, Boolean, Boolean> param1) //for two param
{
    param1.invoke(1, true);
}

//how to call
void main() 
{
    foo(() -> {
        return true;
    });

    foo1((var1) -> {
        return true;
    });

    foo2((var1, var2) -> {
        return true;
    });
        
}

Lambdas纯粹是一个调用站点构造:lambda的接收方不需要知道涉及到lambda,而是接受具有适当方法的Interface。

换句话说,您定义或使用一个函数接口(即具有单一方法的接口),该接口接受并返回您想要的内容。

自Java 8以来,Java .util.function中有一组常用的接口类型。

对于这个特定的用例,有java.util.function.IntBinaryOperator和一个int applyAsInt(int left, int right)方法,所以你可以这样写你的方法:

static int method(IntBinaryOperator op){
    return op.applyAsInt(5, 10);
}

但你也可以定义自己的接口,并像这样使用它:

public interface TwoArgIntOperator {
    public int op(int a, int b);
}

//elsewhere:
static int method(TwoArgIntOperator operator) {
    return operator.op(5, 10);
}

然后调用以lambda作为参数的方法:

public static void main(String[] args) {
    TwoArgIntOperator addTwoInts = (a, b) -> a + b;
    int result = method(addTwoInts);
    System.out.println("Result: " + result);
}

使用您自己的接口的好处是您可以使用更清楚地表明意图的名称。

有一个支持lambda的Java 8 JavaDocs的公共web可访问版本,链接到http://lambdafaq.org/lambda-resources。(这显然应该是对Joachim Sauer的回答的评论,但我无法进入我的SO帐户,我需要声望点来添加评论。)lambdafaq站点(由我维护)回答了这个问题和许多其他Java-lambda问题。

注意:这个答案是在Java 8 GA文档公开之前写的。不过,我把它留在了原处,因为Lambda FAQ对于学习Java 8中引入的特性的人来说可能仍然有用。