How you decide when use ArrayList and LinkedList

How you decide when use ArrayList and LinkedList

Answer: Below are points when ArrayList or LinkedList is better choice:

Scenario: Random access:

  • ArrayList is better choice if you need to access its elements randomly

Reason: Internally ArrayList is list of array and so it has direct references to each and every elements in the list so getting nth postion of elements will be constant all time. On the other hand LinkedList needs to traverse through the list from beginning to reach nth element so It won’t be constant all time.

Scenario: Frequent deletion of its elements:

  • Linked list is better choice.

Reason: As we know ArrayList internally maintains list of array so deleting elements from it means its size will be reorganize and new array will be created and elements will be moved to new list. LinkedList is faster because internally its structure is linked together with the references so deletion would be only adjustment of its references.

Scenario: Frequent insertion of elements:

  • Linked list is good choice.

Reason: As we know ArrayList internally maintains list of array so inserting elements to it means its size will be reorganize and new array will be created and elements will be moved to new list. LinkedList is faster because internally its structure is linked together with the references so insertion would be only adjustment of its references.

That’s it How you decide when use ArrayList and LinkedList

Leave a Reply

Your email address will not be published. Required fields are marked *