This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Try to understando generic methods  RSS feed

 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anybody get to interpret this following code: -> public static <T extends Comparable<? super T>> void sort(List<T> list) ?


André AS
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method accepts a generic list of which the type T must implement (the generic type definition doesn't know implement only extend) Comparable. But the type of Comparable must be T or a super class of T.

Example:
 
Virendrasinh Gohil
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
André Asantos wrote: Does anybody get to interpret this following code: ->

This says, the sort method accept list of Ts where T must implement comparable interface. Later part actually defined the type of Comparable interface. It says, the comparable interface should be of type of it's base class of current class.

Please see the code below for better understanding.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!