在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);
}
}
当前回答
下面是c#如何处理这个问题(但是用Java代码表示)。像这样的东西几乎可以满足你所有的需求:
import static org.util.function.Functions.*;
public class Test {
public static void main(String[] args)
{
Test.invoke((a, b) -> a + b);
}
public static void invoke(Func2<Integer, Integer, Integer> func)
{
System.out.println(func.apply(5, 6));
}
}
package org.util.function;
public interface Functions {
//Actions:
public interface Action {
public void apply();
}
public interface Action1<T1> {
public void apply(T1 arg1);
}
public interface Action2<T1, T2> {
public void apply(T1 arg1, T2 arg2);
}
public interface Action3<T1, T2, T3> {
public void apply(T1 arg1, T2 arg2, T3 arg3);
}
public interface Action4<T1, T2, T3, T4> {
public void apply(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
}
public interface Action5<T1, T2, T3, T4, T5> {
public void apply(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);
}
public interface Action6<T1, T2, T3, T4, T5, T6> {
public void apply(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6);
}
public interface Action7<T1, T2, T3, T4, T5, T6, T7> {
public void apply(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7);
}
public interface Action8<T1, T2, T3, T4, T5, T6, T7, T8> {
public void apply(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8);
}
//Functions:
public interface Func<TResult> {
public TResult apply();
}
public interface Func1<T1, TResult> {
public TResult apply(T1 arg1);
}
public interface Func2<T1, T2, TResult> {
public TResult apply(T1 arg1, T2 arg2);
}
public interface Func3<T1, T2, T3, TResult> {
public TResult apply(T1 arg1, T2 arg2, T3 arg3);
}
public interface Func4<T1, T2, T3, T4, TResult> {
public TResult apply(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
}
public interface Func5<T1, T2, T3, T4, T5, TResult> {
public TResult apply(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);
}
public interface Func6<T1, T2, T3, T4, T5, T6, TResult> {
public TResult apply(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6);
}
public interface Func7<T1, T2, T3, T4, T5, T6, T7, TResult> {
public TResult apply(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7);
}
public interface Func8<T1, T2, T3, T4, T5, T6, T7, T8, TResult> {
public TResult apply(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8);
}
}
其他回答
对于不超过2个参数的函数,可以传递它们而无需定义自己的接口。例如,
class Klass {
static List<String> foo(Integer a, String b) { ... }
}
class MyClass{
static List<String> method(BiFunction<Integer, String, List<String>> fn){
return fn.apply(5, "FooBar");
}
}
List<String> lStr = MyClass.method((a, b) -> Klass.foo((Integer) a, (String) b));
在bifuncfunction <Integer, String, List<String>>中,Integer和String是其参数,List<String>是其返回类型。
对于只有一个形参的函数,可以使用function <T, R>,其中T是它的形参类型,R是它的返回值类型。有关Java已经提供的所有接口,请参阅此页。
这很简单。lambda表达式的目的是实现函数接口。它是只有一个方法的接口。这里有一篇关于预定义和遗留功能接口的很棒的文章。
不管怎样,如果你想实现你自己的功能接口,那就做吧。举个简单的例子:
public interface MyFunctionalInterface {
String makeIt(String s);
}
所以让我们创建一个类,在这里我们将创建一个方法,它接受MyFunctionalInterface的类型:
public class Main {
static void printIt(String s, MyFunctionalInterface f) {
System.out.println(f.makeIt(s));
}
public static void main(String[] args) {
}
}
你应该做的最后一件事是将MyFunctionalInterface的实现传递给我们已经定义的方法:
public class Main {
static void printIt(String s, MyFunctionalInterface f) {
System.out.println(f.makeIt(s));
}
public static void main(String[] args) {
printIt("Java", s -> s + " is Awesome");
}
}
就是这样!
您可以使用如上所述的功能接口。 下面是一些例子
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());
希望能有所帮助
如果你同时使用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;
});
}
对我来说,最有意义的解决方案是定义一个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是可用于实现它的快捷语法的名称。