6

My question is pretty much the oneliner that makes up the title. When is it appropriate to use the List interface instead of the Collection interface?

Is it just a question about clarity and readability, i.e. the intention of the code is clearer if I use either List or Collection depending on my code, or are there some other advantages that I'm unaware of?

Daniel Figueroa
  • 10,348
  • 5
  • 44
  • 66
  • possible duplicate of [What is the difference between List (of T) and Collection(of T)?](http://stackoverflow.com/questions/398903/what-is-the-difference-between-list-of-t-and-collectionof-t) – RamonBoza Oct 10 '13 at 15:56
  • When you are working with `List`. – Sotirios Delimanolis Oct 10 '13 at 15:56
  • If you're sure you will work with `List`s only, then use `List`. If your method should support `List`s, `Set`s and other `Collection`s in general, use `Collection`. – Luiggi Mendoza Oct 10 '13 at 15:57
  • 1
    @RamonBoza that question is for .net, not for Java =\ – Luiggi Mendoza Oct 10 '13 at 15:57
  • 1
    possible duplicate of [What is the difference between Collection and List in Java?](http://stackoverflow.com/questions/3317381/what-is-the-difference-between-collection-and-list-in-java) – Luiggi Mendoza Oct 10 '13 at 15:59

6 Answers6

5

One can argue either way, assuming, of course, that you're not dependent on the methods/features of List, and none of the other methods you'll be calling expect List.

There's the argument that it's best to use the most general type that suits the job, to allow code modification/reuse that might lead to switching to a non-List class.

There's also the argument that it's best to use the most specific type that encompasses all planned uses, to have the most power and flexibility within the domain of the planned uses. Using a more specific type also is a sort of self-documentation in that it indicates the narrowness of the code functionality.

In my experience the benefits of code reuse tend to be overstated, and rarely bear fruit outside of code bases that are developed for multiple use. So I'd tend to favor using the more specific type.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • Amen. Providing more information about an implementation allows others to work with it intelligently. Personally, I'm the type that wants to know when I'm indexing into an array-backed list versus a linked list. – pamphlet Oct 10 '13 at 16:21
  • Thank you for your insightful comments, It was a hard choice between choosing this or @SURESH ATTA's answer, I find these both as very informative, though his is a bit more to the point whilst your answer helps me as a developer in general. Thanks! :) – Daniel Figueroa Oct 15 '13 at 07:25
3

When you need the below benefits:

In addition to the operations inherited from Collection, the List interface includes operations for the following:

Listed as

Positional access — manipulates elements based on their numerical position in the list

Search — searches for a specified object in the list and returns its numerical position

Iteration — extends Iterator semantics to take advantage of the list's sequential nature

Range-view — performs arbitrary range operations on the list.

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

This question does a good job of weighing up the pros and cons. In essence, List applies some extra functionality to a Collection, because List is in fact a Collection.

If I were you, I would have a think about the requirements of your code. If you need only to add and remove items from your collection object, then you should use Collection.

If you wish to preserve ordering, grab items in your list at a specific index or remove an element at a given position, then you should go with List, because Collection doesn't provide this functionality.

Community
  • 1
  • 1
christopher
  • 26,815
  • 5
  • 55
  • 89
1

It just depends, do you want your users to be able to index into the data? If yes, use List. Both are interfaces, so you're not leaking implementation details, really, you just need to decide the minimum functionality needed.

pnathan
  • 713
  • 3
  • 9
0

It depends.

If you don't care what type of collection is being used, then Collection<E> would be suitable. If you need to specify that a type of List<E>, Set<E>, etc. should be used, then specify that.

You could also add Iterable<E> to your question; it all depends on what level of specificity is needed.

bn.
  • 7,739
  • 7
  • 39
  • 54
0

If object which you reference implements List interface(e.g. ArrayList, LinkedList) - of course it's better to use List reference. From my poor experience - I don't remember when I last time saw Collection references in production code. Never needed such abstraction. It's much clearer to use List if it's appropriate.

ka4eli
  • 5,294
  • 3
  • 23
  • 43