While practicing Labdas, I am passing Lambda
s from my main
methods. It runs alright, but IntelliJ
shows warning saying that: Unchecked call to 'lambda(T, T)' as a member of raw type 'my.lambda.closure'
at line 18
. What am I missing here?
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);
}
}
You have defined Closure
here as a raw type.
T getDesired(Closure closure)
What you intended might be
T getDesired(Closure<T> closure)
There is a built in closure called BiPredicate which tests a pair of values.
BTW, max
and min
should be Integer
, I wouldn't change them to Double
Integer max = integerCollection.getDesired((a, b) -> a > b);