1.

When to use ArrayList and LinkedList in Java?

Answer»

The List interface is implemented using the ArrayList and the LinkedList. The ArrayList implements a dynamically RESIZED array while the LinkedList implements a doubly linked list. So ArrayList or LinkedList in Java should be SELECTED ACCORDING to the implementations required. Also the differences between both of these are provided to make the selection easily.

Some of the differences between Arraylist and LinkedList are given as follows:

  1. The time required to find if an element is AVAILABLE in both ArrayList and LinkedList is O(n) in the worst case. But binary search can be used on a sorted ArrayList to reduce this time to O(log n).
  2. It is easier and much faster to insert elements in LinkedList as compared to ArrayList. The time complexity for insertion in ArrayList is O(n) in worst case while it is O(1) in LinkedList.
  3. Deletion of elements is also easier in LinkedList as compared to ArrayList. However the time complexity for deletion in ArrayList and LinkedList is O(n) in worst case.
  4. There is more memory overhead in LinkedList as compared to ArrayList. This is because in LinkedList each node contains the DATA and address of the previous and next nodes while in ArrayList each index contains the data only.


Discussion

No Comment Found