【问题标题】:Passing lambda expression from method... What's wrong with is code?从方法传递 lambda 表达式......代码有什么问题?
【发布时间】:2021-04-30 06:31:10
【问题描述】:
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 的运算符 %

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

【问题讨论】:

    标签: lambda java-8 arguments predicate


    【解决方案1】:

    您需要指定 Predicate 期望的类型。

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 2011-12-28
      • 1970-01-01
      • 1970-01-01
      • 2022-11-27
      • 1970-01-01
      相关资源
      最近更新 更多