Difference between ArrayList and LinkedList in Java
Java ArrayList and Java LinkedList both implements List interface. Both can dynamically grow and makes proper use of memory. Both looks same from outside but there are some advantages of one over other. So, what is the difference between ArrayList and LinkedList and which one to choose for making the better application?
Both maintain insertion order which means when you add elements in an ArrayList or in LinkedList, they will be added in same sequence in which they are added. Same in case of the delete operation.
Difference between Arraylist and LinkedList
As we know, ArrayList maintains an index of each element as it uses array data structure internally whereas, Java LinkedList implements Doubly Linked List which uses two side pointers.
So for search operations, ArrayList performs O(1) operation as it knows an index of an element but in case of LinkedList, it has to search for the specific node first which in worst case maybe performs O(n) operations.
If your application requires more search operations(Lookup), you can choose ArrayList in your application rather than LinkedList.
ArrayList in case of insertion requires more cost in terms of memory as it has to shift elements every time during the insertion operation. In the worst case, it would be O(n) operations while in the best case there will be O(1) operations.
In the case of LinkedList, insertion would be easy as it maintains pointers .
If your application requires more insert operations, you can choose LinkedList in your application rather than ArrayList.
ArrayList deletion also requires more cost in terms of memory as it has to shift elements every time during the delete operation. In the worst case, it would be O(n) operations if that element is last while in the best case there will be O(1) operations if an element is at first position.
In the case of LinkedList, deletion would be easy as it maintains pointers . Just need to change two node pointers.
If your application requires more delete operations, You can choose LinkedList in your application rather than ArrayList.
How was the tutorial? Nice. Right?
Deep Concept videos to crack the highly-piad interviews.
Chekout out our Youtube Channel to get detailed video content on important topics in interviews.
What is your Interview Score?
Test your skillset with the curated questions created by experts around the globe.
Book a free test slot. Now !.
It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.Book a test slot. Now!