2012-04-18 49 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>在同一時間。