Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

collection vs list vs arrays as return type for EJB method

I was recently told that collection should be preferred to List as the return value of an EJB method. The argument is that in general collection is more generic i.e. gives you to change the underlying data structure without impacting the clients. And if this is the flexibility you want to have as a designer then using collection will make better sense. But then wouldn't it make more sense to return just an array instead of collection?

And what are performance impacts if any?

Thanks in advance.

like image 829
Kiran Mohan Avatar asked Jul 12 '11 16:07

Kiran Mohan


1 Answers

  • Prefer collections over arrays; Use generics
  • Use interfaces instead of concrete classes

Then you usually have 4 options: List, Set, Collection and Iterable. There it depends what is the semantics you want to include.

  • If it is an internal API - decide it based on the characteristics of the collection:
    • does it only hold unique items? Set
    • will clients need random access to it? List
    • will clients need to modify it (add, remove) (without the above two characteristics)? Collection
    • will clients just need to iterate it? Iterable
  • If it's a web service it doesn't matter - it is serialized the same way.

(Note: there are some collection interfaces with more specific semantics: Queue, Deque, Map, Bag, Multiset, etc. - but it will be fairly obvious when you need to return them)

like image 183
Bozho Avatar answered Oct 21 '22 07:10

Bozho