127
When to use ArrayList and LinkedList in Java
ArrayList provides constant time for search operation, so it is better to use ArrayList if searching is more frequent operation than add and remove operation. The LinkedList provides constant time for add and remove operations. So it is better to use LinkedList for manipulation.
ArrayList has O(1) time complexity to access elements via the get and set methods.
LinkedList has O(n/2) time complexity to access the elements.
LinkedLinked class implements Deque interface also, so you can get the functionality of double ended queue in LinkedList. The ArrayList class doesn’t implement Deque interface.
In sort, ArrayList is better to access data wherease LinkedList is better to manipulate data. Both classes implements List interface.
ArrayList Example
Output:
Traversing ArrayList... ankit peter mayank
LinkedList Example
Output:
After adding: [ankit, peter, mayank] After removing: [ankit, mayank] After changing: [ankit, vivek]
Next TopicJava Collections Interview Questions