从方法传递 lambda 表达式...代码有什么问题?


import java.util.function.Predicate;
class Test {
// lambda expression can be passed as first argument in the check() method
static boolean check(Predicate ti, int b) {
return ti.test(b);
}
}
public class ClassCuncurrentModExcep {
public static void main(String arg[]) {
// lambda expression
boolean result = Test.check((x) -> (x%2) == 0, 10);
System.out.println("The result is: "+ result);
}
}

在将lambda作为参数传递给Test.check((时,未定义参数类型Object、int的运算符%

创建谓词表达式的标准方法是什么?

您需要指定谓词所期望的类型。

class Test {
// lambda expression can be passed as first argument in the check() method
static boolean check(Predicate<Integer> ti, int b) {
return ti.test(b);
}
}

最新更新