Java - Difference between ArrayList and LinkedList

By Preeti Jain Last updated : February 03, 2024

ArrayList and LinkedList classes are an implementation of List interface in Java. Both are non-synchronized. But there are some differences that you should know.

What is ArrayList?

ArrayList class provides resizable arrays. It is a class of java.util package. ArrayList grows and shrinks dynamically during the runtime which makes it a more flexible data structure.

Package

The following package is required to use ArrayList:

import java.util.ArrayList;

Declare and initialize

The following is the declaration and initialization statement of ArrayList:

ArrayList<String> myArrayList = new ArrayList<>();

Example

This example demonstrates an example of Java ArrayList.

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    // Declare and initialize an ArrayList
    ArrayList < Integer > numbers = new ArrayList < > ();

    // Add elements
    numbers.add(11);
    numbers.add(23);
    numbers.add(45);
    numbers.add(12);
    numbers.add(67);

    // Display
    System.out.println("ArrayList:");
    for (int num: numbers) {
      System.out.println(num);
    }

    // Add an element at a specific index
    numbers.add(1, 100);

    // Display
    System.out.println("\nArrayList after adding one more element:");
    for (int num: numbers) {
      System.out.println(num);
    }

  }
}

The output of the above example is:

ArrayList:
11
23
45
12
67

ArrayList after adding one more element:
11
100
23
45
12
67

What is LinkedList?

LinkedList class provides the implementation of a doubly-linked list data structure. It is a class of java.util package. It is also an implementation of List Interface. In LinkedList, each list element contains the references to the previous and next elements.

Package

The following package is required to use LinkedList:

import java.util.LinkedList;

Declare and initialize

The following is the declaration and initialization statement of LinkedList:

LinkedList<String> myLinkedList = new LinkedList<>();

Example

This example demonstrates an example of Java LinkedList.

import java.util.LinkedList;

public class Main {
  public static void main(String[] args) {
    // Declare and initialize an ArrayList
    LinkedList < Integer > numbers = new LinkedList < > ();

    // Add elements
    numbers.add(11);
    numbers.add(23);
    numbers.add(45);
    numbers.add(12);
    numbers.add(67);

    // Display
    System.out.println("LinkedList:");
    for (int num: numbers) {
      System.out.println(num);
    }

    // Add elements at first and last
    numbers.addFirst(100);
    numbers.addLast(200);

    // Display
    System.out.println("LinkedList after adding elements at first and last:");
    for (int num: numbers) {
      System.out.println(num);
    }

  }
}

The output of the above example is:

LinkedList:
11
23
45
12
67
LinkedList after adding elements at first and last:
100
11
23
45
12
67
200

Difference between ArrayList and LinkedList

Here are the main differences between ArrayList and LinkedList in Java:

ArrayListLinkedList
ArrayList is based on the dynamic array. LinkedList is based on the doubly linked list.
Manipulation in ArrayList is slow due to its dynamic array base. Manipulation in LinkedList is faster as it works on the doubly linked list.
For storing data and sorting it - you should use ArrayList. For storing data and manipulating it - you should use LinkedList.
ArrayList stores data contiguously in the memory. LinkedList does not store data contiguously in the memory.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.