File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
jose chiramal
Ranch Hand
Posts: 266
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the api for java.util.Collections i came across the following syntax :

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


What does the above syntax mean ?
 
Paul Clapham
Sheriff
Pie
Posts: 20161
23
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's six or eight pieces of syntax there, at least. I'm sure you know what many of them are for -- I'm pretty sure you know what public is for, for example. So how about if you tell us which parts you don't understand?
 
jose chiramal
Ranch Hand
Posts: 266
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok fine, what does this mean ?

<T extends Comparable<? super T>>


the argument provided to the sort method should implement Comparable and not extend it?
 
Rajeev Rnair
Ranch Hand
Posts: 308
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jose chiramal wrote:Ok fine, what does this mean ?

<T extends Comparable<? super T>>


the argument provided to the sort method should implement Comparable and not extend it?


We cannot say
<T implements Comparable<? super T>> that is Syntax error. Even if you implement interface, you have to say <T extends Interface> . It is clearly explained in the K&B book generics section , last page or so ( i avn't remember correctly!)
The meaning is T implements Comparable interface!
Also it means all the elements in the List<T> must implement Comparable interface, or it should be mutually comparable.
All Wrapper classes, String, Date etc implements Comparable.

Anotehr example:


Hope this helps!

 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jose chiramal wrote:In the api for java.util.Collections i came across the following syntax :

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

What does the above syntax mean ?


This declaration says, that argument to sort() method must be of a type List<T>,
where T could be any type that implements Comparable<? super T> (sort requires compareTo method defined in Comparable to compare elements of list)
Comparable<? super T> means that type ? passed to Comparable could be T or any supertype of T.

Consider this code:

Class B doesn't implement Comparable<B> (and doesn't define it's own compareTo(B b) method).
Class B inherits compareTo(A x) method from class B (we can say that it implements Comparamble<A>).
And sort(List<B>) compiles fine, it is conforming with declaration: public static <B extends Comparable<? super B>> void sort(List<B> list)
 
rohit chavan
Ranch Hand
Posts: 132
Hibernate Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, the best possible explanation one could find for this.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All, @Ireneusz, fantastic explanation! It couldnt have been better
I have also just discovered something I didnt know -That I would have failed in the real exams- In generic type declaration the wildcard '?' and the 'super' keyword are NOT allowed, but this syntax is making that happen -though in a different way-, what I mean is this;



Will NOT compile. wow, just when you think you've learnt the necessary, new things always come up... Once again good job Ireneusz.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic