blog

Home / DeveloperSection / Blogs / Compare ArrayList and LinkedList in Java.

Compare ArrayList and LinkedList in Java.

Compare ArrayList and LinkedList in Java.

Ravi Vishwakarma 126 19-Jul-2024

ArrayList and LinkedList are two commonly used implementations of the List interface in Java, each with distinct characteristics and use cases. Here's a detailed comparison of ArrayList and LinkedList, including when to use one over the other:

ArrayList

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.

ArrayList is a part of the collection framework. It is present in the java.util package and provides us with 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. 

// 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

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 an 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. 

// 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]

 

Summary

Feature ArrayList LinkedList
Data Structure Dynamic array Doubly linked list
Access Time O(1) O(n)
Insert/Remove Time O(n) (except at the end) O(1) (if node reference known)
Memory Overhead Lower Higher
Use Case Fast access, fewer insertions/removals Frequent insertions/removals

Choosing Between ArrayList and LinkedList:

  • Use ArrayList if you need fast access to elements and can handle the cost of occasional insertions and removals.
  • Use LinkedList if you need frequent insertions and removals and can tolerate slower access times.

Read more

When to use LinkedList over ArrayList in Java?

How to Create a LinkedList<String, int>

When to use LinkedList over ArrayList?


Updated 19-Jul-2024
Hi, my self Ravi Vishwakarma. I have completed my studies at SPICBB Varanasi. now I completed MCA with 76% form Veer Bahadur Singh Purvanchal University Jaunpur. SWE @ MindStick | Software Engineer | Web Developer | .Net Developer | Web Developer | Backend Engineer | .NET Core Developer

Leave Comment

Comments

Liked By