• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Try to understando generic methods

 
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
Saloon Keeper
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic