A Collection is a group of individual objects represented as a single unit. Java provides Collection Framework which defines several classes and interfaces to represent a group of objects as a single unit This framework consists the List Interface as well as the ArrayList class. In this article, the difference between the List and ArrayList is discussed.
List: The List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List interface is implemented by the classes of ArrayList, LinkedList, Vector and Stack. List is an interface, and the instances of List can be created by implementing various classes. The following is an example to demonstrate the implementation of a list:
[Geeks, For, Geeks]
ArrayList: ArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java. This class implements the List interface. Similar to a List, the size of the ArrayList is increased automatically if the collection grows or shrinks if the objects are removed from the collection. Java ArrayList allows us to randomly access the list. ArrayList can not be used for primitive types, like int, char, etc. We need a wrapper class for such cases. The following is an example to demonstrate the implementation of an ArrayList:
[Geeks, For, Geeks]
List vs ArrayList in Java
|List is an Interface.||ArrayList is a Class.|
|List interface extends the Collection framework.||ArrayList extends AbstractList class and implements List interface.|
|List cannot be instantiated.||ArrayList can be instantiated.|
|List interface is used to create a list of elements(objects) which are associated with their index numbers.||ArrayList class is used to create a dynamic array that contains objects.|
|List interface creates a collection of elements that are stored in a sequence and they are identified and accessed using the index.||ArrayList creates an array of objects where the array can grow dynamically.|
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.