Difference between ArrayList and LinkedList in Java

In this post, we will discuss the difference between ArrayList and LinkedList in Java.

This is one of the frequently asked Java interview question in Java interviews so let's discuss the difference between ArrayList and LinkedList in Java.

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


Both ArrayList and LinkedList implement the List interface. However, they differ completely in the way they store and link to the elements.

Following are some key differences between LinkedList and ArrayList:
  • An ArrayList stores the elements sequentially based on their index. However, a LinkedList uses a doubly-linked list to store its elements.
  • A LinkedList consumes more memory than an ArrayList because it also stores the next and previous references along with the data.
  • You can access an element in an ArrayList is O(1) time. But it takes O(n) time to access an element in a LinkedList because it needs to traverse to the desired element by following the next/prev references.
  • Adding or removing elements are usually slower in an ArrayList compared to LinkedList. This is because the elements in the ArrayList needs to be shifted if a new element is added in the middle of the ArrayList. The ArrayList might also need to be resized to accommodate the new element. Similarly, in case of removal, the elements in the ArrayList needs to be shifted to the new positions.

YouTube Video

This difference explained very well in below YouTube video:

Comments