Java – comparable vs. comparable

I don'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 the difference between "up" and "down" bounded wildcards, I still don't understand why they use '? In this case, super t 'is not a simple't' If I use these methods, I will get the same results Any suggestions?

Solution

With your version, the following will not compile:

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

class Derived extends Base { ... }

List<Derived> list = ...;

mySort(list);

Derivation does not extend comparable < derived > However, it does extend comparable < base > (therefore, comparable )

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>