DEV Community

Cover image for When to use LinkedList over ArrayList in Java ?
Rakesh KR
Rakesh KR

Posted on

When to use LinkedList over ArrayList in Java ?

In Java, the LinkedList and ArrayList classes are both used to store and manage a collection of objects. However, there are some differences between the two classes that you should be aware of when choosing which one to use in your code.

Here are some situations where you should use a LinkedList over an ArrayList in Java:

  • If you need to frequently add and remove elements from the middle of the list. LinkedList is implemented as a doubly-linked list, which allows for efficient insertion and removal of elements at arbitrary positions in the list. In contrast, ArrayList is implemented as an array, so inserting or removing an element in the middle of the list requires shifting all of the subsequent elements to make room, which can be slow.

  • If you need to iterate over the elements of the list in both forward and backward directions. Because LinkedList is a doubly-linked list, you can easily move backwards and forwards through the list, which is not possible with an ArrayList, which is a linear structure.

  • If you have a large number of elements and you need to minimize memory usage. LinkedList uses less memory than ArrayList because it only stores the elements and references to the next and previous elements in the list, while ArrayList stores the elements and an array to hold the elements, which requires more memory.

In general, LinkedList is a good choice when you need a more flexible and efficient list data structure, while ArrayList is a better choice when you need a simple and efficient way to store

Top comments (1)

Collapse
 
cicirello profile image
Vincent A. Cicirello • Edited

LinkedLists don't use less memory than an ArrayList. In fact, most of the time they use more.

LinkedList with n elements:

  • n node objects
  • 3n object references (each node has next and previous references and reference to element) + 1 reference to current node = 3n + 1 references.
  • at 4 bytes per object reference, we have approximately 12n+4 bytes

ArrayList with n elements added one at a time:

  • ArrayList class is partially filled array that doubles length of internal array whenever more space needed, so internal array can be as long as 2n in length in this case.
  • Therefore 2n object references + 1 reference to array: 2n + 1 object references.
  • At 4 bytes per reference: 8n+4 bytes.
  • Approximately 33% less memory than LinkedList.

ArrayList with n elements specifying internal array size at time of construction or using trimToSize() method that decreases length to current size:

  • Only needs n object references for n elements + 1 reference to array for n+1 object references.
  • At 4 bytes per reference: 4n+4 bytes.
  • Approximately 66% less memory than LinkedList.

Also traversing an ArrayList backwards is not a problem. Easily done in either direction. Just decrement indexes instead if incrementing.

The only real advantage to LinkedList over ArrayList is inserting or removing from interior of LinkedList.