Comparable<? super T> vs. Comparable<T>

jani777

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?

Oliver Charlesworth

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>).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Comparable<T> vs Raw Comparable

From Dev

Issue with <T extends Comparable<? super T>>

From Dev

Why does T extends Comparable <? super T> include T? Meaning includes Comparable<T>?

From Dev

Explanation of generic <T extends Comparable<? super T>> in collection.sort/ comparable code?

From Dev

Generics in Java - Why I am not allowed to do Comparable<? extends T> instead of Comparable<? super T>?

From Dev

What isn't comparable by the Comparable class?

From Dev

Comparable<T> and compareTo

From Java

ClassCastException when overriding a super's method (Comparable<T>)

From Dev

Java Comparing Generics with Comparable<? super T> from another class

From Dev

Comparable VS <? extends Comparable>

From Dev

java.util.Comparator.naturalOrder takes a <T extends Comparable<? super T>> and returns a Comparator<T> - why?

From Dev

Why does `Class<T> == Boolean.class` cause a compiler error when `T extends Comparable<? super T>`?

From Dev

Should 'Comparable<T>' be a 'Functional interface'?

From Dev

Using Comparable<T> with Generic class

From Dev

How to convert a Comparable[] to a List<T>?

From Dev

Trouble Understanding Comparable<T> cast

From Dev

No instance of type variable(s) T exist so that ID conforms to Comparable<? super T>

From Dev

Can I use <T implements Comparable> or just <Comparable>

From Dev

What does <T extends Comparable<T>> mean?

From Dev

JAXB 2.1 implementing Comparable<T> for the generated Class

From Dev

Why is it not possible to implement Comparable<T> multiple times?

From Dev

implementing compareTo (Comparable<T>) for a Binary Tree(Generic)

From Dev

Why LocalDate doesn't implements Comparable<LocalDate>?

From Dev

Issue implementing the java Comparable<T> class

From Dev

What would be the C# equivalent of <T extends Comparable> implements Comparable<Transition <T>>?

From Dev

Why generic constraint T extends Comparable<T> is not sufficient to avoid a cast?

From Dev

Generic Sorting function accepts T, but want to ensure T is comparable

From Dev

How to extend Node<T extends Comparable<T>> class

From Dev

When to implement Comparable<super class of X> instead of Comparable<X>?

Related Related

  1. 1

    Comparable<T> vs Raw Comparable

  2. 2

    Issue with <T extends Comparable<? super T>>

  3. 3

    Why does T extends Comparable <? super T> include T? Meaning includes Comparable<T>?

  4. 4

    Explanation of generic <T extends Comparable<? super T>> in collection.sort/ comparable code?

  5. 5

    Generics in Java - Why I am not allowed to do Comparable<? extends T> instead of Comparable<? super T>?

  6. 6

    What isn't comparable by the Comparable class?

  7. 7

    Comparable<T> and compareTo

  8. 8

    ClassCastException when overriding a super's method (Comparable<T>)

  9. 9

    Java Comparing Generics with Comparable<? super T> from another class

  10. 10

    Comparable VS <? extends Comparable>

  11. 11

    java.util.Comparator.naturalOrder takes a <T extends Comparable<? super T>> and returns a Comparator<T> - why?

  12. 12

    Why does `Class<T> == Boolean.class` cause a compiler error when `T extends Comparable<? super T>`?

  13. 13

    Should 'Comparable<T>' be a 'Functional interface'?

  14. 14

    Using Comparable<T> with Generic class

  15. 15

    How to convert a Comparable[] to a List<T>?

  16. 16

    Trouble Understanding Comparable<T> cast

  17. 17

    No instance of type variable(s) T exist so that ID conforms to Comparable<? super T>

  18. 18

    Can I use <T implements Comparable> or just <Comparable>

  19. 19

    What does <T extends Comparable<T>> mean?

  20. 20

    JAXB 2.1 implementing Comparable<T> for the generated Class

  21. 21

    Why is it not possible to implement Comparable<T> multiple times?

  22. 22

    implementing compareTo (Comparable<T>) for a Binary Tree(Generic)

  23. 23

    Why LocalDate doesn't implements Comparable<LocalDate>?

  24. 24

    Issue implementing the java Comparable<T> class

  25. 25

    What would be the C# equivalent of <T extends Comparable> implements Comparable<Transition <T>>?

  26. 26

    Why generic constraint T extends Comparable<T> is not sufficient to avoid a cast?

  27. 27

    Generic Sorting function accepts T, but want to ensure T is comparable

  28. 28

    How to extend Node<T extends Comparable<T>> class

  29. 29

    When to implement Comparable<super class of X> instead of Comparable<X>?

HotTag

Archive