Java Tutorial Series - Collections - LinkedList

We have already covered ArrayList topic from Collections in previous tutorial of Java series, in this article we will be covering LinkedList and its advantage over ArrayList.

LinkedList Syntax - 

To begin with the syntax of LinkedList, it can be defined the same way as that of ArrayList

LinkedList<Integer> number = new LinkedList<Integer>();
OR
List<Integer> number = new LinkedList<Integer>();

Advantage of LinkedList on ArrayList -

LinkedList consists of elements where each element has a reference to the previous and next element.

[0] -> [1] -> [2]

If we want to add or remove the elements in beginning or the middle of the List, we would be using LinkedList instead of ArrayList since it is fast when adding elements in middle or the beginning.

Contrary, if we want to add or remove the elements from the end, then ArrayList is most efficient option to do so and not the LinkedList. Adding to it, LinkedList is slower when traversing through the elements because each element passes the reference to the next element which consumes lot of time.

In an nutshell, I would be using ArrayList anytime when dealing with addition or removal of elements at the end of the list instead of going for LinkedList, but when we have to deal with adding or removing the elements in middle or beginning, LinkedList is the best option.

Keep Learning and keep sharing!

Comments

Popular posts from this blog

Azure Tutorials Series - Azure Networking

Testing in CI/CD

Azure Tutorials Series - IaaS vs PaaS vs SaaS