在练习 Labdas 时,我正在通过我的方法传递Lambda
s 。main
它运行正常,但IntelliJ
显示警告说:Unchecked call to 'lambda(T, T)' as a member of raw type 'my.lambda.closure'
at 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);
}
}