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?
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.
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:
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.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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With