Open In App

ArrayList vs LinkedList in Java

Improve
Improve
Like Article
Like
Save
Share
Report

An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. However, the limitation of the array is that the size of the array is predefined and fixed. There are multiple ways to solve this problem. In this article, the difference between two classes that are implemented to solve this problem named ArrayList and LinkedList is discussed.

Java Collections Framework Hierarchy

ArrayList is a part of the collection framework. It is present in the java.util package and provides us dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. We can dynamically add and remove items. It automatically resizes itself. The following is an example to demonstrate the implementation of the ArrayList. 

Example

Java




// Java program to Illustrate Working of an ArrayList
 
// Importing required classes
import java.io.*;
import java.util.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an ArrayList of Integer type
        ArrayList<Integer> arrli
            = new ArrayList<Integer>();
 
        // Appending the new elements
        // at the end of the list
        // using add () method via for loops
        for (int i = 1; i <= 5; i++)
            arrli.add(i);
 
        // Printing the ArrayList
        System.out.println(arrli);
 
        // Removing an element at index 3
        // from the ArrayList
        // using remove() method
        arrli.remove(3);
 
        // Printing the ArrayList after
        // removing the element
        System.out.println(arrli);
    }
}


Output

[1, 2, 3, 4, 5]
[1, 2, 3, 5]

LinkedList is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The elements are linked using pointers and addresses. Each element is known as a node. Due to the dynamicity and ease of insertions and deletions, they are preferred over the arrays. The following is an example to demonstrate the implementation of the LinkedList

Note: This class implements the LinkedList Data Structure.

Example

Java




// Java program to Demonstrate Working of a LinkedList
 
// Importing required classes
import java.util.*;
 
// Main class
class GFG {
 
    // main driver method
    public static void main(String args[])
    {
 
        // Creating an object of the
        // class linked list
        LinkedList<String> object
            = new LinkedList<String>();
 
        // Adding the elements to the object created
        // using add() and addLast() method
 
        // Custom input elements
        object.add("A");
        object.add("B");
        object.addLast("C");
 
        // Print the current LinkedList
        System.out.println(object);
 
        // Removing elements from the List object
        // using remove() and removeFirst() method
        object.remove("B");
        object.removeFirst();
 
        System.out.println("Linked list after "
                           + "deletion: " + object);
    }
}


Output

[A, B, C]
Linked list after deletion: [C]

Now after having an adequate understanding of both of them let us do discuss the differences between ArrayList and LinkedList in Java

  ArrayList LinkedList
1. This class uses a dynamic array to store the elements in it. With the introduction of generics, this class supports the storage of all types of objects. This class uses a doubly linked list to store the elements in it. Similar to the ArrayList, this class also supports the storage of all types of objects.
2. Manipulating ArrayList takes more time due to the internal implementation. Whenever we remove an element, internally, the array is traversed and the memory bits are shifted. Manipulating LinkedList takes less time compared to ArrayList 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.
3. Inefficient memory utilization. Good memory utilization.
4. It can be one, two or multi-dimensional. It can either be single, double or circular LinkedList.
5. Insertion operation is slow. Insertion operation is fast.
6. This class implements a List interface. Therefore, this acts as a list. This class implements both the List interface and the Deque interface. Therefore, it can act as a list and a deque.
7. This class works better when the application demands storing the data and accessing it. This class works better when the application demands manipulation of the stored data.
8. Data access and storage is very efficient as it stores the elements according to the indexes. Data access and storage is slow in LinkedList.
9. Deletion operation is not very efficient. Deletion operation is very efficient.
10. It is used to store only similar types of data. It is used to store any types of data.
11. Less memory is used. More memory is used.
12. This is known as static memory allocation. This is known as dynamic memory allocation.

ArrayList is an implementation of the List interface that uses an array to store its elements. It has a fast indexed access time, which means that retrieving elements from an ArrayList by an index is very quick. 

For example, the following code demonstrates how to retrieve an element from an ArrayList:

Java




import java.io.*;
import java.util.ArrayList;
 
class GFG {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        int thirdNumber = numbers.get(2);
        System.out.println("The third number is: " + thirdNumber);
    }
}


Output

The third number is: 3

In addition, inserting or deleting elements from the middle of an ArrayList can be slow, as the entire array needs to be shifted in memory to make room for the new element or to close the gap left by the deleted element.

For example, the following code demonstrates how to insert an element in the middle of an ArrayList:

Java




import java.io.*;
import java.util.ArrayList;
 
class GFG {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(2, 4);
        System.out.println("The numbers are: " + numbers);
    }
}


Output

The numbers are: [1, 2, 4, 3]

LinkedList, on the other hand, is an implementation of the List interface that uses a linked list data structure to store its elements. 

Unlike an ArrayList, a LinkedList does not use an array to store its elements. Instead, each element in a LinkedList is represented by a node that contains a reference to the data stored in the node and a reference to the next node in the list.



Last Updated : 24 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads