未经检查的调用'lambda(T,T)'作为原始类型'my.lambda.closure'的成员

时间:2016-10-09 13:13:07

标签: java lambda closures unchecked functional-interface

在练习Labdas时,我正在通过Lambda方法传递main。它运行正常,但IntelliJUnchecked call to 'lambda(T, T)' as a member of raw type 'my.lambda.closure'显示警告:line 18。我在这里缺少什么?

package my.lambda;

interface Closure<T extends Number> {
    boolean lambda(T a, T b);
}

class Collection<T extends Number> {
    private T[] numbers;

    Collection(T[] numbers) {
        this.numbers = numbers;
    }

    T getDesired(Closure closure) {
        T desiredSoFar = numbers[0];

        for (T number : numbers) {
            if (closure.lambda(number, desiredSoFar))
                desiredSoFar = number;
        }

        return desiredSoFar;
    }
}

public class Lambda {
    public static void main(String[] args) {
        Integer[] iOb = {1, 2, 3, 12, 4, 5, 6};
        Collection<Integer> integerCollection = new Collection<>(iOb);

        Double max = integerCollection.getDesired((a, b) -> a.doubleValue() > b.doubleValue())
                .doubleValue();
        Double min = integerCollection.getDesired((a, b) -> a.doubleValue() < b.doubleValue())
                .doubleValue();

        System.out.println("Maximum of the integers: " + max);
        System.out.println("Maximum of the integers: " + min);

        Double[] dOb = {1.1, 2.2, 3.3, 12.12, 4.4, 5.5, 6.6};
        Collection<Double> doubleCollection = new Collection<>(dOb);

        max = doubleCollection.getDesired((a, b) -> a.doubleValue() > b.doubleValue());
        min = doubleCollection.getDesired((a, b) -> a.doubleValue() < b.doubleValue());

        System.out.println("Maximum of the doubles: " + max);
        System.out.println("Maximum of the doubles: " + min);
    }
}

1 个答案:

答案 0 :(得分:1)

您已在此处将Closure定义为原始类型。

T getDesired(Closure closure) 

您打算做什么

T getDesired(Closure<T> closure) 

有一个名为BiPredicate的内置闭包,用于测试一对值。

BTW,maxmin应为Integer,我不会将其更改为Double

Integer max = integerCollection.getDesired((a, b) -> a > b);