This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Collections" Watch "Collections" New topic
Author

Collections

Douglas Yesmunt
Greenhorn

Joined: Mar 18, 2003
Posts: 2
Can collections contain collections?
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

Can collections contain collections?

Yes, you can have a HashMap that contains ArrayList objects as entries, and those ArrayList objects can in turn contain HashSets, and so on to infinity (well, until you run out of memory
Eugene.
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

Yes. Collection can contains Collection, of course we are talking about classes that implement Collection interface.
As you will notice the methods in Collection interface like add, or contain take Object as parameter.
So you can pretty much put anything except
primitive data type in it.
Douglas Yesmunt
Greenhorn

Joined: Mar 18, 2003
Posts: 2
Thanks alot for the help
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Collections
 
Similar Threads
Garbage collection - arrays
Collections
List in Descending order
high priority topics in SCJP
collections and generics