문제

I can't see any difference between this default sort method(from java.util.Collections)

public static <T extends Comparable<? super T>> void sort(List<T> list) {
      //implementation
}

..and this :

public static <T extends Comparable<T>> void mySort(List<T> list) {
    //implementation
}

Although I know about the differences between the 'upper' and 'lower' bounded wildcards,I still don't understand why they use '? super T' instead of simple 'T' in this case.If I use these methods,I get the same result with both of them.Any suggestions?

도움이 되었습니까?

해결책

With your version, the following will not compile:

class Base implements Comparable<Base> { ... }

class Derived extends Base { ... }

List<Derived> list = ...;

mySort(list);

Derived does not extend Comparable<Derived>. However, it does extend Comparable<Base> (and thus, Comparable<? super Derived>).

다른 팁

From what I understood, "<? super T>" is a way to recognize that the CompareTo method is not implemented in T itself, but inherited by a SuperClass.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top