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