Array List and Linked List

Let's talk about Java Array List and Linked List here.

Array List:

Features-
  • ArrayList class implements List interface (and a couple more) and extends AbstractList class.
  • ArrayList is a re-sizable array implementation which means it can automatically grow its size if required.
  • ArrayList is internally backed by an Array.
  • Duplicates are allowed.
  • Not thread safe.
How it works:
  • When we instantiate an ArrayList, it actually creates an Array of default capacity of 10 using no argument constructor. 
  • When we add elements to the list, it stores the elements inside that array. Once this array gets full, it automatically creates a new array of larger capacity than the previous array (typically 1.5 time larger) and copies all the elements from old array to the new larger array.
  • However it does not shrink its size if we keep deleting the elements from it.

Linked List:

Features-
  • Doubly Linked List implementation of Linked List (Abstract Data Type) provided by Java.
  • LinkedList class implements following interface:
    • List
    • Deque
    • Clonable
    • Serializable
    • Iterable
    • Collection
    • Queue
  • It extends AbstractSequentialList class.
  • Duplicates are allowed.
  • Uses Nodes to store elements.
  • Dynamic memory allocation i.e. nodes will be created when required.
  • Not thread safe.

No comments: