aspose file tools*
The moose likes Object Relational Mapping and the fly likes Collections: java.util.Collection vs java.util.List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Collections: java.util.Collection vs java.util.List" Watch "Collections: java.util.Collection vs java.util.List" New topic
Author

Collections: java.util.Collection vs java.util.List

Yohan Liyanage
Ranch Hand

Joined: Aug 17, 2007
Posts: 132

Hi,

When defining collections, I've seen some developers prefer to use java.util.Collection, whilst others prefer more specialized types such as java.util.List and java.util.Set. When we use java.util.Collection, Hibernate uses PersistentBag as the implementation class, where as for List and Set, it uses PersistentList, PersistentSet respectively.

In my perspective, I think it's better to use types such as List / Set, because they define certain qualities such as guaranteed ordering, non-duplication, etc (Unless we need to have a Bag).

What do you guys think about this?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

When I am working with Objects, I choose the best Collection class for the object, regardless of using an ORM tool or not.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Yohan Liyanage
Ranch Hand

Joined: Aug 17, 2007
Posts: 132

Hi Mark,

Mark Spritzler wrote:When I am working with Objects, I choose the best Collection class for the object, regardless of using an ORM tool or not.

Mark


So you would go for List / Set / Map depending on the case, instead of always relying on java.util.Collection.

Thanks !
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Yohan Liyanage wrote:Hi Mark,

Mark Spritzler wrote:When I am working with Objects, I choose the best Collection class for the object, regardless of using an ORM tool or not.

Mark


So you would go for List / Set / Map depending on the case, instead of always relying on java.util.Collection.

Thanks !


Yeah that is my tendency. I rarely if ever go more generic as Collection. Doesn't mean that there aren't use cases out there that work or is a better solution.

Mark
tabiul mahmood
Greenhorn

Joined: Sep 17, 2009
Posts: 18
Same here. By going for a general Collections you will lose some specific methods that a detailed implementation provides. So I also feel you should use Collection type that is most suitable for your requirement
 
wood burning stoves
 
subject: Collections: java.util.Collection vs java.util.List