Wednesday, April 18, 2012

Java Collection compare generic class that extends interface that extends comparable

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>>




No comments:

Post a Comment