Difference Between ArrayList VS LinkedList In Java With Example

ArrayList VS LinkedList In Java: In this article, we will discuss the difference between ArrayList and LinkedList classes in detail. Also, we will list a 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 the key differences between these 2 collection classes

ArrayList VS LinkedList

ArrayListLinkedList
To store item/elements, ArrayList uses a dynamic array or dynamically re-sizing array, i.e., an internal data structureTo store items/elements, LinkedList uses a doubly-linked list, i.e., an internal data structure
The initial capacity of ArrayList is 10It doesn’t have any initial capacity, i.e., constructs an 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 an internally new array is created with 50% more of the original size andOld array data copied into the new arrayNo such overhead, as item/element is added to end of LinkedListDue to this, insertion is faster in LinkedList comparing with ArrayList
Similarly, while deleting from the middle of ArrayList involves a lot of shifting workDeletion is much simpler in LinkedList, as previous and next links get deleted and a new link is formed
ArrayList internally uses an array to store the items, so retrieval becomes faster as array works on index-basedLinkedList iterate over a list to retrieve/get the required item/element
Overall, retrieval is faster in ArrayList when comparing with LinkedList other words, if an application requires a lot of retrieval tasks then ArrayList is the best suitOverall, insertion and removal is faster in LinkedList when comparing with ArrayList other words, LinkedList is the best suit for an application involving a 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, LinkedList has more memory overhead as it needs to maintain addresses of the previous and next node in addition to 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

When to use ArrayList?

  • When there are several 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 the new array
  • It is better to avoid ArrayList when there are the number of insertion/removal/deletion of an item/element from ArrayList as it involves a lot of shifting work internally

When to use LinkedList?

  • When there are a number of insertion like for example, whenever airplane lands then its data need to be captured and stored in the list
  • Also when item/element needs to be deleted from a list, then LinkedList is the best fit, when comparing with ArrayList
  • Don’t use LinkedList, when there are a number of retrieval as every item need to be traversed either from beginning/end to get the required item from a list

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment