• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics .. contravariance question on Collections.sort static method

 
Ranch Hand
Posts: 53
Android C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Studying generics now. Just want some confirmation about the collection.sort(List<T> list) method.

public static <T extends Comparable><? super T>> void sort(List<T> list)

I understand the <T extends Comparable> since it indicates the elements in the list must implements comparable interface in order to use this sort method.

Question is here.
Say my list is List <Integer>
So T is Integer. From what i understand <? super T> which is <? super Integer> indicates that the elements in the list must be minimum of Integer.
I understand this ensure that the lower bounded is Integer so that this method wont give a problem.

But if <T extends Integer> .. that would work too. For this case, I know that T can then be anything lower than Integer which might cause a problem if settings is used in the sort method.
My point is <T extends Comparable ><? extends T> will work too since like in my case T is always Integer. Only problem i see using the covariance instead of contra-variance is
like i mention earlier in that sort method there are settings which are forbidden in generics for covariance.
Please advice.
Thanks.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kim, I couldn't properly understand what you are trying to say, but the syntax <T extends Comparable<? super T>> is used because of a reason. Lets take an example

Now if you see, MySubClass actually implements Comparable<MyClass> (through inheritance). This is where the syntax <T extends Comparable<? super T>> is useful. If the syntax of the sort method was <T extends Comparable<T>>, then there would've been a compile time error if we tried to pass a List<MySubClass> to the sort method as T i.e. MySubClass doesn't implement Comparator<MySubClass>. So in the syntax <T extends Comparable<? super T>> if we think that T is MySubClass, then in Comparable<? super T>, the ? becomes MyClass...
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are contra variant generic questions there in exam?
 
reply
    Bookmark Topic Watch Topic
  • New Topic