Compare ArrayList and Linked List
ArrayList |
LinkedList |
ArrayList uses dynamic array to store elements. | LinkedList uses a linked list (Doubly Linked List) to store elements. |
ArrayList is an index-based data structure, where each element is associated with an index. | The elements in the LinkedList are called nodes, each node needs to store three pieces of information: the reference of the previous element, the value of the element, and a reference to the next element. |
Adding and removing elements with ArrayList is slow because it uses arrays internally. Because after adding or removing elements need reordering. | Adding and removing elements with LinkedList is faster than ArrayList. Because it doesn’t need to rearrange the elements after adding or removing. It simply updates the reference to the element before and after it. |
Retrieving elements in ArrayList is faster than LinkedList. Because the elements in the ArrayList are stored based on the index (index). | Retrieving elements in LinkedList is much slower than in ArrayList. Because, it has to iterate through the elements from first to last. |
ArrayList can only act as a list because it only implements the List interface. | LinkedList can act as an ArrayList, stack (queue), queue, Singly Linked List and Doubly Linked List because it implements List and Deque interfaces. |
ArrayList is better at storing and retrieving data (get). | LinkedList is better at data manipulation (add/remove). |