What is the difference between Collection
and List
in Java? When should I use which?
7 Answers
First off: a List
is a Collection
. It is a specialized Collection
, however.
A Collection
is just that: a collection of items. You can add stuff, remove stuff, iterate over stuff and query how much stuff is in there.
A List
adds the information about a defined sequence of stuff to it: You can get the element at position n, you can add an element at position n, you can remove the element at position n.
In a Collection
you can't do that: "the 5th element in this collection" isn't defined, because there is no defined order.
There are other specialized Collections as well, for example a Set
which adds the feature that it will never contain the same element twice.
-
14yep, list is a ordered specialization of collection (but not sorted) +1 Commented Jul 23, 2010 at 11:09
-
2List in java extends Collections interface and builds indexed functions which help in position based retrieval and removal behavior Commented Jul 23, 2010 at 18:46
Collection
is the root interface to the java Collections hierarchy. List
is one sub interface which defines an ordered Collection, other sub interfaces are Queue
which typically will store elements ready for processing (e.g. stack).
The following diagram demonstrates the relationship between the different java collection types:
-
2Really like your image, I've seen it when preparing for SCJP but nearly forget all about those stuff these days. Commented Jul 23, 2010 at 10:59
-
1I must admit that this image was poached from this blog. I too first saw a diagram like this in the K&B SCJP book.– krockCommented Jul 23, 2010 at 11:02
-
you can learn a lot from the scjp cert, most of the books to this topic are awesome :) Commented Jul 23, 2010 at 11:07
-
Java API is the best to answer this
Collection
The root interface in the collection hierarchy. A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered. The JDK does not provide any direct implementations of this interface: it provides implementations of more specific subinterfaces like Set and List. This interface is typically used to pass collections around and manipulate them where maximum generality is desired.
List (extends Collection)
An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
Unlike sets, lists typically allow duplicate elements. More formally, lists typically allow pairs of elements e1 and e2 such that e1.equals(e2), and they typically allow multiple null elements if they allow null elements at all. It is not inconceivable that someone might wish to implement a list that prohibits duplicates, by throwing runtime exceptions when the user attempts to insert them, but we expect this usage to be rare.
List and Set are two subclasses of Collection.
In List, data is in particular order.
In Set, it can not contain the same data twice.
In Collection, it just stores data with no particular order and can contain duplicate data.
Collection is the Super interface of List so every Java list is as well an instance of collection. Collections are only iterable sequentially (and in no particular order) whereas a List allows access to an element at a certain position via the get(int index)
method.
Collection is the main interface of Java Collections hierarchy and List(Sequence) is one of the sub interfaces that defines an ordered collection.
Collection is a high-level interface describing Java objects that can contain collections of other objects. It's not very specific about how they are accessed, whether multiple copies of the same object can exist in the same collection, or whether the order is important. List is specifically an ordered collection of objects. If you put objects into a List in a particular order, they will stay in that order.
And deciding where to use these two interfaces is much less important than deciding what the concrete implementation you use is. This will have implications for the time and space performance of your program. For example, if you want a list, you could use an ArrayList or a LinkedList, each of which is going to have implications for the application. For other collection types (e.g. Sets), similar considerations apply.