I have this interface
public interface IDataPoint<T> extends Comparable<T> {
public T getValue();
}
and this implementation…
public class IntegerDataPoint implements IDataPoint<Integer> {
// ... some code omitted for this example
public int compareTo(Integer another) {
// ... some code
}
}
and another class…
public class HeatMap<X extends IDataPoint<?> {
private List<X> xPoints;
}
Now I would like to use Collections.max (and similar) on the xPoints list, but that does not work, probably because I got my generics all messed up.
Any suggestions how this could be solved (without a Comparator)?
Collections.max(xPoints);
gives me this error:
Bound mismatch: The generic method max(Collection<? extends T>) of type Collections is not applicable for the arguments (List<X>). The inferred type X is not a valid substitute for the bounded parameter <T extends Object & Comparable<? super T>>
The problem is that
Collections.max(Collection<? extends T>)wants the T’s to be comparable to themselves not some other type.In your case
IntegerDataPointis comparable toInteger, but notIntegerDataPointYou cannot easily fix this because
IntegerDataPointis not allowed to implementComparable<Integer>andComparable<IntegerDataPoint>at the same time.