Here you will learn about difference between arraylist and linkedlist in java i.e. arraylist vs linkedlist.
Both ArrayList and LinkedList are similar in many ways like both implement List interface and are non-synchronized. But still there are various differences between them which I have discussed below.
Also Read: Difference between ArrayList and Vector in Java
Difference between ArrayList and LinkedList in Java
|Implementation||ArrayList internally implements dynamic array to store elements.||LinkedList internally implements doubly linked list to store elements.|
|Accessing||An elements can be retrieved or accessed in O (1) time. ArrayList is faster because it uses array data structure and hence index based system is used to access elements.||An element can be accessed in O (n) time. LinkedList is slower because it uses doubly linked list and for accessing an element it iterates from start or end (whichever is closer).|
|Insertion||Normally the insertion of element at end takes O (1) time. But in case array is full then array is resized by copying elements to new array, which makes insertion time O (n).||Insertion of an element at start and end in LinkedList is faster, it takes O (1) time. Inserting at specified position is slower, it takes O (n) time.|
|Deletion||Deletion of an element is slower and takes O (n) time because all the elements from old array is copied to new array.||Deletion of element from start and end is faster and takes O (1) time. While deletion at specified position is slower and takes O (n)|
|Memory||Memory consumption in ArrayList is less as it uses index based system to store elements.||Memory consumption in LinkedList is more. In each node extra space is required to store address of previous and next node.|
|Initial Capacity||By default ArrayList creates a list of initial capacity 10.||LinkedList creates an empty list without initial capacity.|
What to Use When?
- As access or get operation is faster in ArrayList so it is used in the scenario where more access or get operations are required.
- As insertion or deletion of an element is faster in LinkedList so it is used in the scenario where more insertion and deletion operations are required.
Comment below if you have queries or found any information incorrect or missing in above tutorial for difference between ArrayList and LinkedList.
2 thoughts on “Difference between ArrayList and LinkedList in Java”
See http://javachannel.org/posts/linkedlist-vs-arraylist/ – there are a lot of implications about what is said here that are not correct.
TL;DR – Always use ArrayList or ArrayDeque. There are very few situations in which LinkedList is appropriate.
I have updated the information, thanks for mentioning.