File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Collections class ( Generics ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Collections class ( Generics )" Watch "Collections class ( Generics )" New topic
Author

Collections class ( Generics )

Steven Rodeo
Ranch Hand

Joined: Mar 06, 2008
Posts: 72

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 ?


Brandon DuRette
Smart Bear Support
Greenhorn

Joined: Jul 06, 2009
Posts: 8
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.


Code Review Tools - Code Review and More - The Smart Bear Blog
Steven Rodeo
Ranch Hand

Joined: Mar 06, 2008
Posts: 72

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

best
_S
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

Here is the sun tutorial on the subject...

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

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
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){}


SCJP 1.5(97%) My Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collections class ( Generics )
 
Similar Threads
new SCJP in town :)
BinarySearch()
First synchronization issue
java.lang.ClassCastException: Employee cannot be cast to java.lang.Comparable