• 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

Collections.sort() API syntax

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am trying to understand Collections.sort() method from Java API,

Given method:1. List<T> means --> List of elements that are of type "City" [or Employee, student etc..]
2. T extends Comparable means --> City implements Comparable
3. What is the meaning of <? super T> here ?
4. What is the meaning of <T extends Comparable<? super T>> ?

Given method1. What is the meaning of <T> before void method ?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a fairly complicated method declaration. However you should be able to comprehend it for SCJP/OCPJP. I would advise you to read the generics tutorial.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saibabaa Pragada wrote:Hi, I am trying to understand Collections.sort() method from Java API,

Given method:1. List<T> means --> List of elements that are of type "City" [or Employee, student etc..]
2. T extends Comparable means --> City implements Comparable
3. What is the meaning of <? super T> here ?
4. What is the meaning of <T extends Comparable<? super T>> ?



From the Java API for Collections.sort()-
Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface. Furthermore, all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list).

Comparable<? super T>- Is to facilitate the comparison between the elements with in the list. The elements which Comparable will compare must extend from the same parent.

<? Super T>- All the elements must extend from T.

You would better refer to the Generics chapter from the Kathy Sierra and Bert Bates SCJP Guide. It has explained this. Or if you want to go in deep about Generics- You can refer to SCJP Guide by Khalid Mughal (it might be too much for the exam, but will give you a crisp understanding)

Saibabaa Pragada wrote:
Given method1. What is the meaning of <T> before void method ?



<T>- This is to mention that the Type T being used in the sort() is different from that, if any, present in the class.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohammed sanaullah wrote:<? Super T>- All the elements must extend from T.


Well, It's "Any type above T or T itself", not any sub types of T.

 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vijitha Kumara wrote:

mohammed sanaullah wrote:<? Super T>- All the elements must extend from T.


Well, It's "Any type above T or T itself", not any sub types of T.



Sorry for the mistake. This always confuses me.
reply
    Bookmark Topic Watch Topic
  • New Topic