我们如何在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 教师闺蜜祝福语简短