ArrayList vs LinkedList in Java, when to use what?

Saikrishna A
2 min readJun 18, 2024

--

Photo by Paico Oficial on Unsplash

Ever wondered which implementation of List class to use while declaring an object in java, should you use ArrayList or should you go with LinkedList.

Let’s get some better understanding now.

ArrayList is backed up by the properties of an Array whereas the LinkedList is backed up by the properties of a Linked List.

In case of an ArrayList similar to array, new Array of a fixed size will be allotted to the object when we create it and as the size increases and crosses a certain threshold JVM creates a new Array with the new size and copies data from the current Array to the new Array and points the ArrayList to the new Array.

Similar to Array, in ArrayList as well, if we know the index of an element inside it we can directly get its value in constant time.

In case of LinkedList similar to Linked List, each object will be linked to the last object in the existing LinkedList as they come. As size increases this process continues to go on.

Similar to Linked List, when we want to get the value of an element even if we know after how many positions the object is attached, we need to traverse the LinkedList object till that position and then get the value.

LinkedList uses more memory compared to ArrayList, since it needs to maintain the links between nodes.

If the indices are cached ArrayList can provide you a constant read time.

In summary, if your List keeps on growing always then it is recommended to use the LinkedList else you can use the ArrayList.

Thanks and Happy Coding !

--

--