Collections – List – ArrayList and LinkedList

Collections – List – ArrayList and LinkedList:
So far we have already discussed regarding Set and Map. In this article we are going to discuss regarding Lists. There are two types of Lists – ArrayList and LinkedList – (HashSet here and HashMap here)

collections

 

Difference Between ArrayList and LinkedList: 

ArrayList LinkedList
ArrayList uses array to store the elements LinkedList uses doubly linked list to store the elements
ArrayList needs to know the size or it will re-create when it needs to grow LinkedList grows dynamically
ArrayList Manipulation is slow since it is an array LinkedList Manipulation is fast


When should we use ArrayList and When to use LinkedList:
ArrayList:

  • When random access of elements are needed
  • If we know the size of array ahead so we can allocate the memory

LinkedList:

  • When we need more insertions/deletions
  • When we do not know the size to be allocated

Example Program:

Output:
op

 HashSet here
HashMap here

 

Leave a Reply

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