List vs ArrayList

Java provides datatypes to deal with collections and distinguishes between the protocol used, i.e., its interface and its implementation. For example, ArrayList is a class implementing the List interface. But ArrayList is not the only class, there exists Vector and LinkedList. The list interface and its methods are described in the corresponding Javadoc documentation.

When using a list, you should always use the interface List for the variable declarations and the result and the input parameters of methods using/returning lists. For example a field containing a list should be declared as:

List books = new ArrayList();
and not as
ArrayList books = new ArrayList();
The reason is information hiding. One wants to hide the information about what implementation class one has used for lists. This allows one later to replace the implementation of ArrayList by another, for a given application, more efficient implementation of list.


On to Iterating over lists Part of Collections
Hubert Baumeister
April 23, 2018