aspose file tools
The moose likes Java in General and the fly likes How can we instantiate a collection that contains any object. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How can we instantiate a collection that contains any object." Watch "How can we instantiate a collection that contains any object." New topic
Author

How can we instantiate a collection that contains any object.

Pavan Kumar
Ranch Hand

Joined: Jan 23, 2004
Posts: 78
Hello Friends,

I have written a simple utility method to split a collection of Strings into reasonable batches of collections. This was quite useful. And I see that the same can be used in other places of the application, where a similar logic is necessary but the collection may contain different object than String. Prior to generics, this was easy.

Could you please suggest some ways to generalize my program to accept a collection of any object type, and return a List of Collection of the type specified?

here's my code


I've tried modifying the method parameter and the List that holds the current batch of objects to the following



So I think my question is, how can I instantiate a Collection that can hold any object?
Thanks,
Cnu.


formerly known as Cnu
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
I think what you want is simply


"I'm not back." - Bill Harding, Twister
Pavan Kumar
Ranch Hand

Joined: Jan 23, 2004
Posts: 78
Thanks Very much Jim, for your help.

I've modified my method to the following, and it works like a charm.


could you perhaps throw some light on this expression, please?


Believe me, I have the generics tutorial open in another tab, but I definitely want to get a heads up on that.

Thanks,
Cnu
[ December 09, 2007: Message edited by: cnu sri ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
[B][Cnu]: could you perhaps throw some light on this expression, please?
[/B]

This is part of the declaration of a generic method (see section 5 of the Generics Tutorial). The original<E> tell us two things: (a) this is a generic method, and (b) there is a single type parameter in this generic method, and its name is E. E could be any type - it's like having a ? wildcard, except that if E appears more than once, it refers to the same type everywhere.

The List<Collection<E>> is just a return type. It's like saying the method returns a List<Collection<?>>, where ? is whatever the type was of the Collection<E> input parameter. If you pass in a Collection<String>, you get back a List<Collection<String>>. If you pass in a Collection<Integer>, you get back a ist<Collection<Integer>>.
Pavan Kumar
Ranch Hand

Joined: Jan 23, 2004
Posts: 78
Jim, Thank you for the explanation and pointer to the appropriate section in the tutorial.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can we instantiate a collection that contains any object.
 
Similar Threads
Simple ArrayList conundrum?
Question about Generics and <? extends T>
Generating all the permutations nad combinations of the given alphabets
Tale of a test driven challenged programmer.