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 or storing of an item/element {add(itemValue)}
  • Removing an item/element {remove(index)}
  • Retrieval of an item/element {get(index)}

 

Lets us move on and discuss key differences between these 2 collection classes

ArrayList LinkedList
To store item/elements, ArrayList uses dynamic array or dynamically re-sizing array i.e.; internal data structure To store items/elements, LinkedList uses doubly linked list i.e.; internal data structure
The initial capacity of ArrayList is 10 LinkedList 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 work Deletion 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-based LinkedList iterate over list to retrieve/get the 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 and removal is faster in LinkedList when comparing with ArrayList

In other words, LinkedList is the best suit for an application involving lot of insertion or 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/elements LinkedList 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 location Elements of LinkedList stored in random memory location

 

When to use ArrayList?

  • When there are more number of retrievals like accessing employee records against the 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

 

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

 

References:

https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html
Read also:

Happy Coding !!
Happy Learning !!