• 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 class ( Generics )

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

HI folks,

the Collections class as documented below

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html

has several methods that has a signature of type

the return types of the methods below all have <T> / <K,V> etc.. they are not the return type. What does it signify ?


 
Smart Bear Support
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These are the methods' type parameter lists. The type parameter list declares the type variables used by the method, separate from the type variables used by the containing class. Practically speaking it is used to bind the types in the generic arguments to the types in the method's generic return type.
 
Steven Rodeo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sorry I can't comprehend your response. Please elaborate with examples if possible

best
_S
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the sun tutorial on the subject...

http://java.sun.com/docs/books/tutorial/extra/generics/methods.html

Henry
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To expain this I would like to use simple analogy
the difference between
public <T>void add(List<T> list){}
public void add(List list){}

above two is same as difference between defining generic and normal array list .

List<T> list = new ArrayList<T>();
List list = new ArrayList();

This type of method is know as generic method and T is know type parameter. This is mainly used to make method polymorphic. Now the question is, we can use wildcard (?) operator as well to make method polymorphic. Generic method declaration is preferred in the following scenarios.
1. where return type and argument type uses same type parameter or related.
2. if arguments of method has some dependency
e.g. public <T> void addList(List<T> list1, List<? extends T> list2){}
 
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic