List vs. ArrayList in Java

In Java, the List interface and the ArrayList class are foundational components of the Java Collection Framework. While they are closely related, it’s crucial to understand the distinctions between them.

List in Java

List is an interface in the Java Collection Framework that represents an ordered collection of elements. It defines a contract for collections that maintains the order of elements and allows access by index. Being an interface, List does not specify how the data is stored or manipulated; instead, it provides a set of operations that can be implemented by various classes.

Key characteristics of List

List has the following characteristics:

  • Ordered collection: List maintains the order in which elements are added.

  • Allows duplicates: List can contain duplicate elements.

  • Provides random access: Elements in a List can be accessed by index.

  • Flexible implementations: List has multiple implementations, including ArrayList, LinkedList, and others.

Code example

Let’s understand List implementation with ArrayList and LinkedList:

import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
public class ListInJava {
public static void main(String[] args) {
// List with ArrayList implementation
List<String> arrayListJava = new ArrayList<>();
arrayListJava.add("Jack");
arrayListJava.add("Ashley");
arrayListJava.add("Martin");
System.out.println("ArrayList in Java: " + arrayListJava);
// List with LinkedList implementation
List<String> linkedListJava = new LinkedList<>();
linkedListJava.add("Xander");
linkedListJava.add("Yasmine");
linkedListJava.add("Zach");
System.out.println("LinkedList in Java: " + linkedListJava);
}
}

Code explanation

  • Lines 1–3: We import key classes and interfaces used in the code. Specifically, they import List, ArrayList, and LinkedList from the Java Collection Framework. List is an interface for ordered collections while ArrayList and LinkedList are two common implementations of List.

  • Line 9: We create a List variable named arrayList with an ArrayList implementation. This variable will hold a list of strings.

  • Lines 10–11: Here, we add three elements: Alice, Bob, and Charlie to the arrayList.

  • Line 15: We create a List variable named linkedList with a LinkedList implementation. This variable will also hold a list of strings.

  • Lines 16-18: Here, we add three elements: Xander, Yasmine, and Zach to the linkedList.

ArrayList in Java

ArrayList is a specific implementation of the List interface that uses an underlying array to store elements. It is part of the java.util package and is known for its dynamic resizing capabilities. As elements are added or removed, ArrayList automatically adjusts its size to accommodate changes.

Key characteristics of ArrayList

The following are the characteristics of ArrayList in Java:

  • Resizable array: ArrayList automatically grows or shrinks as elements are added or removed.

  • Fast random access: Since ArrayList uses an array-based structure, accessing elements by index is fast.

  • Shifting elements: While ArrayList is efficient for random access, adding or removing elements in the middle of the list can be costly due to the need to shift elements.

Code example

Let’s understand ArrayList implementation with the help of an example:

import java.util.ArrayList;
public class ArrayListJava {
public static void main(String[] args) {
// Create an ArrayList of integers
ArrayList<Integer> ArrayListOfNumbers = new ArrayList<>();
// Add elements to the ArrayList
ArrayListOfNumbers.add(10);
ArrayListOfNumbers.add(20);
ArrayListOfNumbers.add(30);
System.out.println("ArrayList after adding elements: " + ArrayListOfNumbers);
// Removes an element from the ArrayList
ArrayListOfNumbers.remove(1); // Removes the second element (20)
System.out.println("ArrayList after removing an element: " + ArrayListOfNumbers);
}
}

Code explanation

  • Line 1: We import the ArrayList class from the java.util package.

  • Line 7: We create an ArrayList named numbers to hold integer values. At this point, the list is empty.

  • Lines 10–12: We add three integer elements to the numbers list: 10, 20, and 30. This is done using the add() method, which appends elements to the end of the list.

  • Line 14: Here, we print the current contents of the ArrayList after adding the elements.

  • Line 17: We remove an element from the ArrayList at index 1. This removes the second element, which is 20.

  • Line 19: This statement prints the ArrayList after removing an element.

Differences Between List and ArrayList


List

ArrayList

Nature

List is an interface.

ArrayList is a class of Java Collection framework.

Flexibility

List allows us to choose different implementations.

ArrayList is a specific implementation that uses an array-based structure.

Performance

Some List implementations, such as LinkedList, might offer better performance for certain operations.

ArrayList is efficient for random access but might require shifting elements when adding or removing items.

Implementation

The interface of a List provides an extension to the Collection Framework.

The ArrayList provides an extension to the List Interface.

Quiz

Test your understanding of this topic. This will help you assess how well you’ve grasped the key concepts we’ve discussed.

1

What is the output of the following Java code?

import java.util.ArrayList;
import java.util.List;

public class ListMutation {
    public static void main(String[] args) {
        List<String> list1 = new ArrayList<>();
        list1.add("A");
        list1.add("B");

        List<String> list2 = list1;
        list2.add("C");

        System.out.println(list1);
    }
}
A)

["A", "B"]

B)

["A", "B", "C"]

C)

["C"]

D)

It will throw an exception.

Question 1 of 30 attempted

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved