Reputation: 63
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?
Upvotes: 3
Views: 386
Reputation: 1
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.
Upvotes: 0
Reputation: 272657
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>
).
Upvotes: 7