[英]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. 现在,我想在xPoints
列表上使用Collections.max(和类似的东西),但这不起作用,可能是因为我弄乱了所有的泛型。
Any suggestions how this could be solved (without a Comparator
)? 有什么建议可以解决这个问题(没有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. 问题在于Collections.max(Collection<? extends T>)
希望T与自己具有可比性, 而不是其他类型。
In your case IntegerDataPoint
is comparable to Integer
, but not IntegerDataPoint
在您的情况下, IntegerDataPoint
与Integer
相当,但与IntegerDataPoint
不IntegerDataPoint
You cannot easily fix this because IntegerDataPoint
is not allowed to implement Comparable<Integer>
and Comparable<IntegerDataPoint>
at the same time. 您不能轻松地解决此问题,因为不允许IntegerDataPoint
同时实现Comparable<Integer>
和Comparable<IntegerDataPoint>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.