2

私はこのインターフェースを持っています

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;
}

リストでCollections.max(および同様のもの)を使用したいのxPointsですが、ジェネリックがすべて台無しになっているためか、それは機能しません。

これを(なしで)どのように解決できるかについての提案はあります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>>
4

1 に答える 1

4

問題は、Tが他のタイプではなく自分自身Collections.max(Collection<? extends T>)に匹敵することを望んでいることです。

あなたの場合IntegerDataPointはに匹敵しますIntegerが、IntegerDataPoint

同時にIntegerDataPoint実装することは許可されていないComparable<Integer>ため、これを簡単に修正することはできません。Comparable<IntegerDataPoint>

于 2012-04-18T14:20:11.543 に答える