7

An ArrayList, a List and a Collection in Java behaves almost in the same way and they have many methods in common. If such is a case then, what are the major differences among them? The following simple code snippet demonstrates it.

package collections;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

final public class Main
{
    public static void main(String[] args) 
    {
        Collection<String> collection=new ArrayList<String>();
        List<String>list=new ArrayList<String>();
        ArrayList<String>arrayList=new ArrayList<String>();        

        collection.add("A");
        collection.add("B");

        list.add("A");
        list.add("B");

        arrayList.add("A");
        arrayList.add("B");

        Iterator<String>collectionIterator=collection.iterator();
        Iterator<String>listIterator=list.iterator();
        Iterator<String>arrayListIterator=arrayList.iterator();

        while(collectionIterator.hasNext())
        {
            System.out.println(collectionIterator.next());
        }

        while(listIterator.hasNext())
        {
            System.out.println(listIterator.next());
        }

        while(arrayListIterator.hasNext())
        {
            System.out.println(arrayListIterator.next());
        }
    }
}

When and where to use an ArrayList, a Collection and a List? In which specific situations, the differences among them may be remarkable?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Bhavesh
  • 4,607
  • 13
  • 43
  • 70
  • 3
    Maybe he knows so much that he thinks the question is not useful... don't get personal by taking it personal. Anyways, your question can probably answered by a couple of other questions, such as [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) and [What is a List vs. an ArrayList?](http://stackoverflow.com/questions/6867484/java-what-is-a-list-vs-an-arraylist). – Felix Kling Nov 06 '11 at 17:37

5 Answers5

14

A Collection is an interface that defines the highest-level of shared collection behavior, and extends Iterable (which just defines the iterator() method).

A List is an interface that defines the highest-level of shared List behavior.

ArrayList is an implementation of List and in general wouldn't be used in a declaration unless you need an implementation guarantee (e.g., fast indexed access), but is fine to use as a list value.

Read the docs to see the differences–they're described in the API. The implementation (ArrayList) will have a type-specific implementation of each method in each interface it implements.

In general, use the least-specific interface that provides what you need. For example, passing Collection instead of List allows a wider selection of implementations. If you need something like indexed access (e.g., List.get(int)), a Collection wouldn't work.

As with most things, which to use depends entirely upon your needs.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
9

For local variables, it does not really matter. It does matter when the type is part of an API, i.e. in a method signature that might be called by code not yet written:

  • The type Collection has a very simple contract - it implies basically only that you have a bunch of elements, you can add and remove elements, check whether the collections contains a specific element, and iterate over them. If that's all your code needs, use Collection - it gives the code that uses your API the greatest freedom.
  • The type List has in its contract that the elements have an order that will not change except through explicit manipulation, that you can get() the n'th element, that add() puts new elements at the end, etc. If your code depends on any of these properties, you should use List
  • ArrayList is just a concrete implementation of List and should never be used in an API. Its usage should only ever be an implementation detail (unless you have some very specific requirements).

Not thinking carefully about the types used in an API can lead to a lot of pointless conversions and unnecessarily slow code as collections are converted between Collection, Set, List and various implementation classes just to satisfy badly designed APIs.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
3

ArrayList is an implementation, Collection and List are interfaces.

Collection is the somewhat super-interface, with List as a specialisation (ordered Collection). ArrayList is a implementation of List (resizable array).

See the Java Tutorials, Trail: Collections, Implementations.

jeha
  • 10,562
  • 5
  • 50
  • 69
2

You're using an ArrayList in all 3 cases. To further explain, Collection<String> collection=new ArrayList<String>(); could be re-written as,

Collection<String> collection;
collection = new ArrayList<String>();

The first statement defines a variable of type Collection<String> named collection while the second statement creates a new ArrayList<String>() and assigns it to collection.

The compiler allows assigning an ArrayList<String> to Collection<String> because ArrayList is-a Collection i.e. all ArrayLists are Collections because they implement the Collection interface. The inverse is obviously not true, all Collections are definitely not ArrayLists.

Sahil Muthoo
  • 12,033
  • 2
  • 29
  • 38
1

Collection and List are interfaces. ArrayList is a class. We cannot new Collection() or new List(). But we can new ArrayList().

Collection has no concept of index. List and ArrayList has a method that can set/get using an index. For example, Collection has add(object). List and ArrayList have add(object) and add(index, object).

wannik
  • 12,212
  • 11
  • 46
  • 58