-2

Possible Duplicate:
What is the difference between List (of T) and Collection(of T)?
What is the difference between Collection and List in Java?

Is there any pros/cons of using one again the other? what's main advantage to use Collection rather than List?

Community
  • 1
  • 1
user705414
  • 20,472
  • 39
  • 112
  • 155
  • The documentation would answer this question rather effectively: http://download.oracle.com/javase/6/docs/api/java/util/List.html. I quote: *[a List is] 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..* – Mark Peters Oct 14 '11 at 03:35
  • Also, what do you mean by "use Collection rather than List"? How were you planning on instantiating the collection? What concrete implementation would you be evaluating? – Mark Peters Oct 14 '11 at 03:39
  • Sorry, accidentally cast my close vote for a .NET question. Should have been: http://stackoverflow.com/questions/3317381/what-is-the-difference-between-collection-and-list-in-java – Matt Ball Oct 14 '11 at 03:43

3 Answers3

1

Basically, the List interface allows you to do operations involving indexes (positions): retrieve the element in a given index, delete an element in an index, insert an element, etc.

A Collection is a more general interface, with more classes implementing it.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
1

Take a look at the JavaDocs for List.

A lot of them have lines like this, showing that they're from the Collection interface:

Specified by:

size in interface Collection<E>

The ones with no "specified by" section are:

E get(int index)

E set(int index,
      E element)

void add(int index,
         E element)

E remove(int index)

int indexOf(Object o)

int lastIndexOf(Object o)

ListIterator<E> listIterator()

ListIterator<E> listIterator(int index)

List<E> subList(int fromIndex,
                int toIndex)

Where E is the Type specified in List<Type>.

Basically, it's a bunch of things related to indexes -- because not all Collections have an index, or an order at all for that matter, and a couple related to special iterators, and subList.

The advantage to using Collection in method signatures is that you don't force your users to use one kind of collection (some users can use a Set, some can use a List, etc.). This only makes sense if you don't need the methods that `List gives you.

In this example, I'm not using any List-specific methods:

/**
 * Simple example, adds "1" to the Collection
 */
public static void addOne(Collection in) {
    in.add(1);
}

There's no reason to force users of this method to only pass it a list, because the only method we're calling on it (add) is available in all Collections.

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
0

A List is a Collection, but it adds the concept of having element order.
In addition to the methods of Collection, List's also have these methods:

  • public boolean addAll(int index, Collection c)
  • public E get(int index)
  • public E set(int index, E element)
  • public void add(int index, E element)
  • public E remove(int index)
  • public int indexOf(Object o)
  • public int lastIndexOf(Object o)
  • public ListIterator listIterator()
  • public ListIterator listIterator(int index)
  • public List subList(int fromIndex, int toIndex)

In general,

  • Use a List when order matters, and/or duplicates are OK. ArrayList is a good choice.
  • Use a Set when order doesn't matter and when duplicates are not OK. HashSet is a good choice.
  • Use a LinkedHashSet when order does matter and duplicates are not OK
Bohemian
  • 412,405
  • 93
  • 575
  • 722