Difference between ArrayList and LinkedList in Java

In this article, we will discuss the difference between ArrayList and LinkedList in Java. This is one of the frequently asked Java interview questions in Java interviews.

Java provides us with two List implementations, ArrayList and LinkedList, to store and manipulate a list of objects. While they both are implementations of the List interface and share some properties, they also have some significant differences. Here, we will take a deep dive into the differences between ArrayList and LinkedList in Java and discuss the most appropriate use cases for each.

YouTube Video

This difference is explained very well in below YouTube video:

Difference between ArrayList and LinkedList in Java

Underlying Data Structure 

The most fundamental difference between ArrayList and LinkedList lies in the underlying data structure. ArrayList internally uses a dynamic array to store its elements. When the array becomes full, a new array is created, and the old array is copied into the new one, which allows ArrayList to resize dynamically.
List<String> arrayList = new ArrayList<>();
System.out.println(arrayList);  // Output: [Apple, Banana]
On the other hand, LinkedList internally used a doubly-linked list to store the elements.
List<String> linkedList = new LinkedList<>();
System.out.println(linkedList);  // Output: [Apple, Banana]

Memory Usage 

ArrayList: In ArrayList, elements are stored in contiguous memory locations, which results in less memory usage. 

LinkedList: In LinkedList, each element stores two extra fields for the address of the next and the previous node, thus consuming more memory.

Performance: Access, Insertion, and Deletion 

ArrayList: ArrayList provides faster access to elements since it uses an array data structure and elements can be accessed directly using their indices. However, insertion and deletion operations are slower in ArrayLists because, during these operations, the array may need to be resized or all elements may need to be shifted.

LinkedList: In contrast, LinkedList provides faster insertion and deletion operations. Inserting or removing an element only requires changing the pointer location in the two adjacent nodes of the linked list. However, access operations are slower because to access an element, we have to start from the head (or tail) and traverse the list until we find it.

When to Use ArrayList and LinkedList?

The decision to use ArrayList or LinkedList in Java depends on your specific use case, but here are some general guidelines: 

Use ArrayList when: 

Random Access is required: ArrayList provides faster access to any element since it maintains an index-based system. 

Less insertion and deletion operations: If your primary operations are fetching elements and you have fewer insert and delete operations, ArrayList offers a better choice. This is because removal or insertion in ArrayList requires shifting of elements, which is costly. 

Use LinkedList when: 

Frequent insertion/deletion: LinkedList is preferable when you have more insertion or deletion operations. Manipulation with LinkedList is faster than ArrayList because it uses a doubly linked list, so no bit shifting is required in memory. 

Fewer search operations: If there are fewer get() and set() operations in your application, LinkedList could be a better choice. 

Memory is not a constraint: As we know, LinkedList consumes more memory than ArrayList, so if memory is not a constraint, then LinkedList can be considered.


The below diagram summaries the between ArrayList and LinkedList in Java:

Related Java Interview Articles