在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是可用于实现它的快捷语法的名称。
其他回答
使用lambda作为参数具有灵活性。它支持java中的函数式编程。基本语法是
Param -> method_body
下面是一种方法,您可以定义一个方法,将函数接口(使用lambda)作为参数。 a.如果你想在函数接口中定义一个方法, 例如,函数接口作为参数/参数提供给从main()调用的方法
@FunctionalInterface
interface FInterface{
int callMeLambda(String temp);
}
class ConcreteClass{
void funcUsesAnonymousOrLambda(FInterface fi){
System.out.println("===Executing method arg instantiated with Lambda==="));
}
public static void main(){
// calls a method having FInterface as an argument.
funcUsesAnonymousOrLambda(new FInterface() {
int callMeLambda(String temp){ //define callMeLambda(){} here..
return 0;
}
}
}
/***********Can be replaced by Lambda below*********/
funcUsesAnonymousOrLambda( (x) -> {
return 0; //(1)
}
}
FInterface fi = (x) ->{返回0;}; funcUsesAnonymousOrLambda (fi); 在上面我们可以看到,lambda表达式是如何被接口替换的。
以上解释了lambda表达式的一个特殊用法,还有更多。 裁判 Java 8 lambda内部的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;
});
}
做以下…
你已经声明了方法(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不是一个对象,而是一个功能接口。 使用@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。
要使用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));
}
}