[英]Java Collection compare generic class that extends interface that extends comparable
我有这个界面
public interface IDataPoint<T> extends Comparable<T> {
public T getValue();
}
还有这个实现
public class IntegerDataPoint implements IDataPoint<Integer> {
// ... some code omitted for this example
public int compareTo(Integer another) {
// ... some code
}
}
还有另一堂课
public class HeatMap<X extends IDataPoint<?> {
private List<X> xPoints;
}
现在,我想在xPoints
列表上使用Collections.max(和类似的东西),但这不起作用,可能是因为我弄乱了所有的泛型。
有什么建议可以解决这个问题(没有Comparator
)?
Collections.max(xPoints);
给我这个错误:
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>>
问题在于Collections.max(Collection<? extends T>)
希望T与自己具有可比性, 而不是其他类型。
在您的情况下, IntegerDataPoint
与Integer
相当,但与IntegerDataPoint
不IntegerDataPoint
您不能轻松地解决此问题,因为不允许IntegerDataPoint
同时实现Comparable<Integer>
和Comparable<IntegerDataPoint>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.