List and ArrayList are the members of Collection framework. List is a collection of elements in a sequence where each element is an object and elements are accessed by there position (index). ArrayList creates a dynamic array of objects that increases or reduces in size whenever required. The primary difference between List and ArrayList is that List is an interface and ArrayList is a class. Let us study the difference between the List and ArrayList with the help of comparison chart shown below.
Content: List Vs ArrayList
|Basis for Comparison||List||ArrayList|
|Basic||List is an Interface||ArrayList is a standard Collection Class.|
|Syntax||interface List||class ArrayList|
|Extend/Implement||List interface extends Collection Framework.||ArrayList extends AbstractList and implements List Interface.|
|Work||It is used to create a list of elements (objects) which are associated with their index numbers.||ArrayList is used to create a dynamic array that contains objects.|
Definition of List
List is an interface which extends the Collection framework. List interface describes the collection of elements that are arranged sequentially. List interface is implemented by the following standard collection classes like ArrayList, LinkedList, CopyOnWriteArrayList, Vector, Stack. List interface has elements that are associated with their index numbers. You can access an element in the list by its position (index) in the list. A list created using List interface start with a zero based index.
In addition to the methods inherited by Collection framework, List interface also defines some method of its own. The methods added by the List interface are, add(int, E) and addAll(int, Collection). These methods add an element to the list by their index. Methods inside the list may throw an exception like UnsupportedOperationException if the method is unable to modify the list. When one object in a list is incompatible with another object in the list, then ClassCastException is thrown.Null elements are not allowed in the list if you try to insert a null object in the list, NullPointerException is thrown.
You can obtain an element from the list using get() method. You can set the value of an element in the list using set() method. You can also get the sublist from the list using a method sublist(). It becomes convenient to operate on the sublist instead of a list.
Definition of ArrayList
One of the standard Collection classes is ArrayList which extends AbstractList class and also implements the List interface. The ArrayList class is used to create the dynamic arrays that grow and shrunk whenever required. The list created using ArrayList class is nothing but the array of objects. In Java, the standard array has the fixed length, so, you must know the size of the array in advance. But, it may be the case that you may not know what length of the array you need until the run time. Hence, Collection framework introduced the ArrayList class to overcome this issue.
ArrayList has constructors that create the array with its intial capacity. Though the capacity of the object of class ArrayList increases automatically when elements are added to the array, still you can manually increase the capacity of the object of ArrayList using the method ensureCapacity(). It is better to increase the capacity of array initially instead of reallocating the memory later. Because reallocation is costlier than allocating the memory at once.
Key Differences Between List and ArrayList
- One of the most important differences between List and ArrayList is that list is an interface and ArrayList is a standard Collection class.
- List interface extends the Collection framework whereas, the ArrayList extends AbstractList Class and it implements List interfaces.
- The namespace for List interface is System.Collection.Generic whereas, the namespace for ArrayList is System.Collection.
- List interface creates a collection of elements that is stored in a sequence and are identified or accessed by their index number. On the other hand, ArrayList creates an array of objects where the array can dynamically grow when required.
The ArrayList overcomes the issue of a static array in standard Java i.e. the array can not grow in size once it is created. When an array is created using ArrayList, a dynamic array is created that can grow and shrink in size when needed. The standard Collection class ArrayList extends the List interface.