Java – ArrayList v/s LinkedList

In this article, we will discuss difference between ArrayList and LinkedList classes in detail i.e.; ArrayList v/s LinkedList

Also, we will list few pointers with regards to below operations

  • Adding/Storing of an item/element {add(itemValue)}
  • Removing an item/element {remove(index)}
  • Retrieval of an item/element {get(index)}

1. ArrayList v/s LinkedList:

ArrayListLinkedList
To store item/elements, ArrayList uses dynamic array or dynamically re-sizing array i.e.; internal data structureTo store items/elements, LinkedList uses doubly linked list i.e.; internal data structure
The initial capacity of ArrayList is 10LinkedList doesn’t have any initial capacity i.e.; just constructs empty list of size 0
When ArrayList exceeds its capacity, then its  size increases by 50%No such thing required in LinkedList
When ArrayList exceeds its capacity, then internally new array is created with 50% more of the original size and

 

Old array data copied into new array

No such overhead, as item/element is added to end of LinkedList

 

Due to this, insertion is faster in LinkedList comparing with ArrayList

Similarly, while deleting from the middle of ArrayList involves lot of shifting workDeletion is much simpler in LinkedList, as previous and next links gets deleted and new link is formed
ArrayList internally uses array to store the items, so retrieval becomes faster as array works on index-basedLinkedList iterate over list to retrieve/get required item/element
Overall, retrieval is faster in ArrayList when comparing with LinkedList

 

In other words, if any application requires lot of retrieval tasks then ArrayList is the best suit

Overall, insertion/removal is faster in LinkedList when comparing with ArrayList

 

In other words, LinkedList is the best suit for an application involving lot of insertion/deletion tasks

There are no memory overhead in ArrayList as it holds only actual item/elements (data)When compared with ArrayList, LinedkList has more memory overhead as it need to maintain addresses of previous and next node in addition to actual actual item/elements (data)
ArrayList can be traversed in only one direction while iterating over its item/elementsLinkedList has an API to traverse in both directions while iterating over its item/elements i.e; using descendingIterator() method
Elements of ArrayList stored in consecutive memory locationElements of LinkedList stored in random memory location

Q) When to use ArrayList ?

  • When there are more number of retrievals like accessing employee records against employee code
  • Insertion and deletion is very less (or very minimal) for an application
  • Reason: when ArrayList capacity exceeds, then internally a new array with 50% more than original size is created and older array data/items/elements are copied into new array
  • It is better to avoid ArrayList, when there are more number of insertion/removal/deletion of an item/element from ArrayList as it involves lot of shifting work internally
  • Read more about ArrayList with details and examples

Q) When to use LinkedList ?

  • When there are more number of insertion like for example, whenever an aeroplane lands then its data need to be captured and stored into the list
  • Also when item/element need to be deleted from list then LinkedList is the best fit, when comparing with ArrayList
  • Don’t use LinkedList, when there are more number of retrieval as every items need to be traversed either from beginning/end to get the required item from list
  • Read more about LinkedList with details and examples

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Spring MVC - Interview question on ModelAndView and DispatcherServlet
Java - Interview program on String using toString() method