Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic