在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);
}
}
在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);
}
}
当前回答
对我来说,最有意义的解决方案是定义一个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是可用于实现它的快捷语法的名称。
其他回答
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);
}
使用您自己的接口的好处是您可以使用更清楚地表明意图的名称。
做以下…
你已经声明了方法(l) 您所要做的就是创建一个名为lambda的Interface,并声明一个抽象方法
public int add(int a,int b);
方法名称在这里不重要。
所以当你调用MyClass时。方法((a, b) - > a + b) 这个实现(a,b)->a+b将被注入到你的接口add方法中,所以当你调用l.add时,它将接受这个实现并执行a和b的加法并返回l.add(2,3)将返回5。 -基本上这就是lambda的作用。
基本上,要将lambda表达式作为参数传递,我们需要一个可以保存它的类型。就像我们在原始int或integer类中持有的整数值一样。Java没有单独的lambda表达式类型,而是使用接口作为类型来保存参数。但是这个接口应该是一个功能接口。
要使用Lambda表达式,您需要创建自己的函数接口,或者使用Java函数接口进行需要两个整数并作为值返回的操作。IntBinaryOperator
使用用户定义的功能接口
interface TwoArgInterface {
public int operation(int a, int b);
}
public class MyClass {
public static void main(String javalatte[]) {
// this is lambda expression
TwoArgInterface plusOperation = (a, b) -> a + b;
System.out.println("Sum of 10,34 : " + plusOperation.operation(10, 34));
}
}
使用Java函数接口
import java.util.function.IntBinaryOperator;
public class MyClass1 {
static void main(String javalatte[]) {
// this is lambda expression
IntBinaryOperator plusOperation = (a, b) -> a + b;
System.out.println("Sum of 10,34 : " + plusOperation.applyAsInt(10, 34));
}
}
您可以使用如上所述的功能接口。 下面是一些例子
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());
希望能有所帮助