Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

In Java, How do you decide when to use ArrayList and When to use LinkedList?

user-image
Question added by Salauddin Mohammad , Sr. Manager, Software Development , Aspen Technology Inc
Date Posted: 2014/08/27
Mohd shahnawaz khan
by Mohd shahnawaz khan , Associate Project , Cognizant Technology Solution

LinkedList is not as popular as ArrayList but still there are situation where a LinkedList is better choice than ArrayList in Java. Use LinkedList in Java if:

 

1) Your application can live without Random access. Because if you need nth element in LinkedList you need to first traverse up to nth element O(n) and than you get data from that node.

 

2) Your application is more insertion and deletion driver and you insert or remove more than retrieval. Since insertion or removal doesn't involve resizing its much faster than ArrayList.

 

Alabbas Alhaj Ali
by Alabbas Alhaj Ali , Senior Frontend Developer , Acomodeo

Accessing elements are faster with ArrayList, because it is index based. But accessing is difficult with LinkedList. It is slow access. This is to access any element, you need to navigate through the elements one by one. But insertion and deletion is much faster with LinkedList, because if you know the node, just change the pointers before or after nodes. Insertion and deletion is slow with ArrayList, this is because, during these operations ArrayList need to adjust the indexes according to deletion or insetion if you are performing on middle indexes. Means, an ArrayList having10 elements, if you are inserting at index5, then you need to shift the indexes above5 to one more.

pardeep kumar
by pardeep kumar , Senior R & D Engineer , Mavenir Systems

Use LinkedList when you need fast insertion and deletion whereas which is slow in ArrayList

Use ArrayList when you need fast iteration and fast random access whereas which is slow in LinkedList

OMAR ABDULWAHAB
by OMAR ABDULWAHAB , programmer , knone

personally ; i don't care about speed,  computers is fast enough nowadays ......    :)-

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.