ArrayList and LinkedList Differences

by|inArticles||3 min read
ArrayList and LinkedList in Java<br>
ArrayList and LinkedList in Java<br>

ArrayList and LinkedList are both classes in Java that are used to store collections of elements, but they have some key differences that make them better suited for certain types of operations.

ArrayList in Java

ArrayList is implemented as an array, which means that it has a fixed size and elements are stored in contiguous memory locations. This makes it very fast for operations such as retrieving an element at a specific index or iterating through the elements in order.

Limitations of an ArrayList in Java

Adding or removing elements from an ArrayList can be slow. The reason for this is its fixed size. If we want to remove an element, we need to create another ArrayList with a bigger size and then copy over all elements from the old ArrayList to the new one, taking into account the new element and it's position in the new ArrayList.

The same principle applies when you remove an element from an ArrayList. We can not just remove the element, we need to create a new ArrayList with a reduced size, then again copy over all elements except the one we would like to remove.

Of course an ArrayList has methods to insert and remove elements (as it implements the Collection Interface). But in general, under the hood we are performing the action that were described above. Depending on the size of an ArrayList those actions can be very expensive in both means, CPU execution time and memory consumption when both arrays are still referenced in the memory.

LinkedList in Java

LinkedList is implemented as a linked list, which means that each element is stored in a separate node that contains a reference to the next and previous elements.

LinkedList example<br>
LinkedList example<br>

Advantages of a LinkedList in Java

The nature of nodes - with the corresponding references to the next and previous node - makes it very efficient for operations such as adding or removing elements of the list, since only the references between nodes need to be updated.

Limitations of a LinkedList in Java

However, it can be slower for operations that require traversing the entire list, such as iterating through the elements or finding an element at a specific index.

For instance, if we would like to access an element in the middle of a long list, we need to traverse all nodes until we find the element we are searching for.

Another consideration

Conclusion

In summary, ArrayList is best suited for situations where you need to perform many read operations and few write operations, such as reading a large amount of data from a file. LinkedList is best suited for situations where you need to perform many write operations and few read operations, such as adding or removing elements from a list as a user interacts with it.

In addition, ArrayList is also more memory efficient than LinkedList as it stores elements in contigous memory locations but LinkedList uses extra memory to store the address of next and previous elements in the form of pointers.

Both ArrayList and LinkedList are powerful classes that can be used to store collections of elements, but they have different strengths and weaknesses that make them better suited for different types of operations. It's important to choose the right data structure for the task at hand to ensure optimal performance and memory usage.

Thank you for reading this far! Let’s connect. You can @ me on Twitter (@debilofant) with comments, or feel free to follow. Please like/share this article so that it reaches others as well.

© Copyright 2023 - ersocon.net - All rights reservedVer. 388