Sunday, May 5, 2019

ArrayList Vs LinkedList In Java

Q- What is the difference between ArrayList and LinkedList?

ArrayList LinkedList
    ArrayList uses dynamic array to store elements     internally.
    ArrayList implements a List interface. Therefore,     this acts as a list.
    LinkedList uses doubly Linked List to store     elements internally.
    LinkedList implements the List interface and     the Deque interface. Therefore, it can act as a     List, Stack and Queue.
    ArrayList is slower then LinkedList  due to the     internal implementation. if we insert or delete an     element, internally, the array is traversed and the     memory bits are shifted. And
    it may take O(n)
    ArrayList if we insert or delete element     because, in a doubly-linked list, there is no     concept of shifting the memory bits. The list is     traversed and the reference link is changed.
    it will take O(1), as it internally uses doubly     LinkedList
    Storing  and accessing the data, is faster in     ArrayList as uses array internally.
    which is index based. So here time complexity is     O(1)
    Search is slower in LinkedList as uses doubly     Linked List
    internally So here time complexity is O(n)
    Manipulation with ArrayList is slow.     It give better performance for Manipulation of     the stored data.
    The memory location for the elements of an     ArrayList is contiguous.     The location for the elements of a linked list is     not contagious.
    The default capacity of ArrayList is 10     There is no case of default capacity in a     LinkedList. In LinkedList,  an empty list is     created when a LinkedList is initialized.
    ArrayList is a resizable array.     No need to resize. It will create new node and     add.

No comments:

Post a Comment