Difference between List and ArrayList
The Java Collection provides an architecture to deal with the group of objects. The collection means a single unit of objects. It allows us to store and manipulate the group of objects as a single unit.
We can easily perform many operations such as searching, sorting, insertion, deletion, and manipulation on a group of objects using the collection framework.
Java Collection framework provides several interfaces and classes. The interfaces contain Set, List, Queue, Deque, and classes contain ArrayList, Vector, LinkedList, HashSet, TreeSet, LinkedHashSet, and PriorityQueue.
In this section, we will differentiate two elements of the Collection framework, which is List and ArrayList. The List is an interface, and ArrayList is a class.
The List is a child interface of the Collection framework that allows us to maintain the ordered collection of the objects. We can store the duplicate values and null elements using List. It provides an index-based method to perform the insert, delete, search, update operations on the objects.
The List interface takes place in java.util package. It implements ArrayList, LinkedList, Stack, and Vector classes. It is also a base of ListIterator classes using which we can iterate the List in forward and backward directions.
List interface provides positional access and insertion of elements and preserves the insertion order. The instance of the List can be created using the implementing classes.
Implementation of List:
We can implement the List interface by using the ArrayList, LinkedList, Vector, and Stack classes.
To instantiate the List interface, we can use the following syntaxes:
Consider the below example of List:
[Hello, Java, Tpoint]
ArrayList class uses a dynamic array for storing the elements. The ArrayList class acts like an array, but there is no size limit in it. We can add or remove the elements whenever we want. We can store the duplicate element using the ArrayList class. It manages the order of insertion internally.
The ArrayList class is much more flexible than the traditional array. It implements the List interface to use all the methods of List Interface. It takes place in Java.util package.
The ArrayList class inherits the AbstractList class and implements the List Interface. The elements of it can be randomly accessed. It can not be used for primitive types such as int, char, etc.; for these data types, we need a wrapper class.
Implementation of ArrayList:
An ArrayList class inherits all the methods of AbstractList class and implements the List interface.
ArrayList class can be declared as follows:
ArrayList can be defined as follows:
Consider the below example of ArrayList:
[Hello, Java, Tpoint]
Some Key Differences Between List Interface and ArrayList Class
Some Major differences between List and ArrayList are as follows:
When we define the List as follows:
It means we can only call the methods and reference members from the List interface.
If we define the ArrayList as follows:
It means we can invoke available methods in ArrayList and use its members in addition to the List.
Consider the below table for some head comparisons between List and ArrayList:
The List is an interface, and the ArrayList is a class of Java Collection framework. The List creates a static array, and the ArrayList creates a dynamic array for storing the objects. So the List can not be expanded once it is created but using the ArrayList, we can expand the array when needed.
It is better to use the List Interface if you want to take advantage of the polymorphism. In the future, if we are required to implement the interface, we will not need to change the program.