2012-04-18 54 views
2

我有这个接口Java集合比较扩展,扩展接口相当的通用类

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

你的意思是'不起作用'是什么意思?你的compareTo没有被调用? – 2012-04-18 14:14:20

+0

请定义“不起作用”。 – Vincent 2012-04-18 14:16:16

+0

增加了一些细节... – Mickel 2012-04-18 14:17:00

回答

4

的问题是,Collections.max(Collection<? extends T>)希望T的是堪比自己没有一些其他类型。

在你的情况IntegerDataPoint媲美Integer,但不IntegerDataPoint

你不能轻易解决这个问题,因为IntegerDataPoint不允许执行Comparable<Integer>Comparable<IntegerDataPoint>在同一时间。