作为原始类型 'my.lambda.closure' 的成员未经检查地调用 'lambda(T, T)'
Unchecked call to 'lambda(T, T)' as a member of raw type 'my.lambda.closure'
在练习 Labdas 时,我正在通过 main
方法传递 Lambda
s。它运行正常,但 IntelliJ
显示警告说:Unchecked 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);
}
}
您已在此处将 Closure
定义为原始类型。
T getDesired(Closure closure)
您的意图可能是
T getDesired(Closure<T> closure)
有一个名为 BiPredicate 的内置闭包,用于测试一对值。
顺便说一句,max
和min
应该是Integer
,我不会把它们改成Double
Integer max = integerCollection.getDesired((a, b) -> a > b);
在练习 Labdas 时,我正在通过 main
方法传递 Lambda
s。它运行正常,但 IntelliJ
显示警告说:Unchecked 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);
}
}
您已在此处将 Closure
定义为原始类型。
T getDesired(Closure closure)
您的意图可能是
T getDesired(Closure<T> closure)
有一个名为 BiPredicate 的内置闭包,用于测试一对值。
顺便说一句,max
和min
应该是Integer
,我不会把它们改成Double
Integer max = integerCollection.getDesired((a, b) -> a > b);