Difference Between List and ArrayList in Java


A collection framework in Java consists of a set of classes and interfaces which helps in implementing various data structures. List and ArrayList belong to this collection framework. List is an interface and ArrayList is a class. Their main motto is to create a list data structure. List is a collection of ordered elements arranged using the concept of indexing. Arrays use the concept of dynamic array to store elements.

What is a List in Java?

A list is one of the interfaces of the collection framework. It stores objects in an ordered manner. Here, the elements are stored sequentially. A list allows duplicate values to store in it. In Lists, elements insertion, deletion, update and search operations are carried out using index-based method. As it preserves the index of the elements stored, it is easy to access them.

You can use a List interface with the help of java.util package. List interface can be implemented by the classes like LinkedList, ArrayList, vector, stack, etc. These classes are used to implement the concept of listing in Java. ListIterator classes are also based on this List which enable us to iterate through the list. The instances of this interface are created using those classes.

Each and every element in a list has an index. Using this index, it become easy to access that element. This indexing of elements starts with Zero. Along with the methods inherited in collection framework, Lists also have their own methods. They are "add (int, E)" and "addAll (int, collection)". These methods are used to add an element into the list based on their index. It also has get(), set(), and sublist() methods used to access the elements, set the value, and to create a sublist of the given list.

What is an ArrayList in Java?

An ArrayList is an extension of abstract class that implements the List interface. ArrayList is similar to that of an array except that its size increases and decreases automatically whenever the elements are added or removed from it. It follows the concept of dynamic array. An Arraylist can store same or different elements.

Using ArrayList, we can insert, update, delete elements at any index. Insertion and deletion operations in ArrayList are slower than that of List. When we insert a new element to an existing ArrayList, the elements below that are shifted to the next index. Similarly, deletion of an element leads to shifting of elements to the previous indexes which is a time taking process.

ArrayList is best for searching operation. It fetches the results faster. Its indexing also starts from zero.

ArrayList has methods to perform various operations. Some of them are −

  • add() − used to add elements into ArrayList

  • get() − used to access an element

  • set() − used to modify an existing element

  • remove() − used to delete or remove an element from ArrayList

  • clear() − used to clear all elements in an ArrayList

Here are some of the advantages of using ArrayLists −

  • ArrayLists have dynamic size. They automatically increase or decrease their size based on the elements entered.

  • ArrayLists can store null elements in them. They also allow duplicate values.

  • One can insert or delete elements from a particular index.

  • We can easily access elements from a desired index

  • ArrayLists can store multiple data types.

  • ArrayLists provide various methods to manipulate the elements present in them. Elements can be accessed in both forward and from backward directions

Following are some of the disadvantages of using ArrayLists −

  • Inserting or deleting an element in an ArrayList is a slow process, as it involves complete shifting of data in them

  • ArrayLists can’t hold primitive data types such as int, float, etc. They can only hold object types

Differences: List and ArrayList in Java

The following table highlights the major differences between List and ArrayList in Java −

List

ArrayList

List is an interface of the collection framework

ArrayList is one of the class that implements List interface

It extends the collection framework

It extends the Abstract class

Lists create a list of elements that can be accessed using indexes

ArrayLists create dynamic arrays that can also be accessed using their indexes

System.Collection.Generic is the namespace for List interface

System.collection is the namespace for ArrayList

It is used to create an ordered list of elements which can be accessed using their indexes

It is used to create a dynamic array of elements

Insert and delete operations are faster in list than that of ArrayList

Insert and delete operations are slower in ArrayLisst

Searching is slower in List

It allows faster searching of an element

A List can’t be instantiated

An ArrayList can be instantiated

Conclusion

A list is an interface of the collection framework, whereas an ArrayList is a class which is used to implement the concept of lists. ArrayLists are widely used due to their dynamic nature. Normal arrays can store values of only a single data type which is not the case with ArrayLists. ArrrayList can store values of different data types.

Updated on: 23-Jun-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements