Javatpoint Logo
Javatpoint Logo

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.

Difference between List and ArrayList

List Interface

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:

Output:

[Hello, Java, Tpoint]

ArrayList Class

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:

Output:

[Hello, Java, Tpoint]

Some Key Differences Between List Interface and ArrayList Class

Some Major differences between List and ArrayList are as follows:

  • One of the major differences is that List is an interface and ArrayList is a class of Java Collection framework.
  • The List extends the collection framework, comparatively ArrayList extends AbstractList class and implements the List interface.
  • The namespace for the List and ArrayList is System.Collection.Generic and System Collection, respectively.
  • List stores elements in a sequence and are identified by the individual index number. Comparatively, ArrayList stores the elements in a dynamic array; it can grow when required.

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:

Base of Comparison List ArrayList
General It is an interface. It is class.
Work It creates a list of objects that can be accessed by the individual index number. It creates a dynamic array that can be expanded when needed.
Implementation List <data-type> list1= new ArrayList(); ArrayList myList = new ArrayList();
Extend/Implement It extends the collection framework. It extends AbstractList class and implements the List interface.
Base Package Java.util Java.util
Namespace System.Collection.Generic System.Collection
Performance It provides faster manipulation of objects. It provides slow manipulation on objects compared to List.
Instantiation It can not be instantiated. It can be instantiate

Conclusion

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.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA