我们如何在Java方法中传递lambda表达式?
在方法中传递的 Lambda表达式具有功能接口类型的参数。如果我们需要传递lambda表达式作为参数,则接收lambda表达式参数的参数类型必须是功能接口类型。
在下面的示例中,可以在参数类型为“TestInterface”的方法中传递lambda表达式。
示例
interface TestInterface {
boolean test(int a);
}
class Test {
//lambda表达式 can be passed as first argument in the check() method
static boolean check(TestInterface ti, int b) {
return ti.test(b);
}
}
public class LambdaExpressionPassMethodTest {
public static void main(String arg[]) {
//lambda表达式
boolean result = Test.check((x) -> (x%2) == 0, 10);
System.out.println("The result is: "+ result);
}
}输出结果
The result is: true
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短