Difference Between ArrayList and LinkedList

Collections are useful for storing data. In a normal array, the array size is fixed. Sometimes it is required to create arrays that can grow as needed. Programming languages such as Java has collections. It is a framework with a set of classes and interfaces. It serves as a container for a group of elements. Collections allow to store, update , retrieve set of elements. It helps to work with data structures such as lists, sets, trees and maps. The list is an interface of Collection framework. ArrayList and LinkedList are two classes in the collections framework . They implement the collection interface and List interface. This article discusses the difference between ArrayList and LinkedList. ArrayList is a class that extends the AbstractList and implements the List interface, which internally uses a dynamic array to store data elements. LinkedList is a class that extends the AbstractSequentialList and implements List, Deque, and Queue  interfaces, which internally use a doubly linked list to store data elements. That is the key difference between ArrayList and LinkedList.

CONTENTS

1. Overview and Key Difference
2. What is ArrayList
3. What is LinkedList
4. Similarities Between ArrayList and LinkedList
5. Side by Side Comparison – ArrayList vs LinkedList in Tabular Form
6. Summary

What is ArrayList?

The ArrayList class is used to create dynamic arrays. Unlike a normal array, the size of a dynamic array is not fixed. An object created using ArrayList class is allowed to store a set of elements in the list. The capacity increases automatically, so the programmer can add elements to the list. The ArrayList class extends the AbstractList class that implements List interface. Therefore, the methods of the List interface can be used by ArrayList. To access elements, the get() method is used. The add() method can be used to add elements to the list. The remove() method is used to remove an element out of the list. Refer the below program.

Figure 01: Example of ArrayList

According to the above program, an object of ArrayList is created.  Using the add method, elements can be added dynamically. The elements “A”,”B”,”C”,”D” and “E” are added using the add method. The remove method is used to remove an element from the list. When passing 4 to the remove method, the letter in the 4th index which is “E” is removed from the list. When iterating through the list using the for loop, the letters A,B,C and D will print.

What is LinkedList?

Similar to ArrayList, the LinkedList is used to store data elements dynamically. An object created using LinkedList class is allowed to store a set of elements in the list. The capacity increases automatically, so the programmer can add elements to the list. It internally uses doubly linked list to store data. In a doubly linked list, the data is stored as nodes. Each node contains two links. The first link points to the previous node. The next link points to the next node in the sequence.

The LinkedList class extends the AbstractSequentialList class and implements the List interface. Therefore, the methods of List interface can be used by the LinkedList. The get() method can be used to access elements of the list. The add() method can be used to add elements to the list. The remove() method is used to remove an element out of the list. Refer the below program.

Figure 02: Example with LinkedList

According to the above program, an object of LinkedList is created.  Using the add method, elements can be added dynamically. The elements “A”,”B”,”C”,”D” and “E” are added using the add method. The remove method is used to remove an element from the list. When passing 4 to the remove method, the letter in the 4th index which is “E” removes from the list. When iterating using the for loop, the letters A,B,C and D will print.

What are the Similarities Between ArrayList and LinkedList?

  • Both ArrayList and LinkedList implement List interface.
  • Both ArrayList and LinkedList can contain duplicate elements.
  • Both ArrayList and LinkedList maintain the insertion order.

What is the Difference Between ArrayList and LinkedList?

ArrayList vs LinkedList

ArrayList is a class that extends the AbstractList and implements the List interface which internally uses a dynamic array to store data elements. LinkedList is a class that extends the AbstractSequentialList and implements List, Deque, Queue  interfaces, which internally uses a doubly linked list to store data elements.
 Accessing Elements
Accessing elements of ArrayList is faster than of a LinkedList. Accessing elements of LinkedList is slower than of an ArrayList.
Manipulating Elements
Manipulating elements of ArrayList is slower than of a LinkedList. Manipulating elements of LinkedList is faster than of an ArrayList.
 Behavior
ArrayList performs as a List. LinkedList performs as a List and a Queue.

Summary – ArrayList vs LinkedList 

The collection framework allows working with data structures such as lists, trees, maps and sets. The list is an interface of collection framework. This article discussed the difference between ArrayList and LinkedList. ArrayList is a class that extends the AbstractList and implements the List interface that internally uses a dynamic array to store data elements. LinkedList is a class that extends the AbstractSequentialList and implements List, Deque, Queue  interfaces, which internally uses a doubly linked list to store data elements. That is the difference between ArrayList and LinkedList.

Reference:

1.Introduction to Linked List | Data Structure Tutorial | Studytonight. Available here
2.“LinkedList in Java – javatpoint.”,The Point, Available here
3.“Java ArrayList class – javatpoint.” The Point, Available here