Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I am working with Objects, I choose the best Collection class for the object, regardless of using an ORM tool or not.

Mark
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic