2012-04-18 9 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は呼び出されていませんか? –

+0

「動作しません」と定義してください。 – Vincent

+0

詳細を追加しました... – Mickel

答えて

4

問題はCollections.max(Collection<? extends T>)はTさんが自分自身ない他のいくつかのタイプと同等になりたいということです。あなたのケースでは

IntegerDataPointIntegerに匹敵するではなく、IntegerDataPoint

IntegerDataPointが同時にComparable<Integer>Comparable<IntegerDataPoint>を実装するために許可されていないので、あなたは簡単にこの問題を解決することはできません。

関連する問題