Fundamentals 7 min read

When to Use List.of vs Arrays.asList in Java: Immutable vs Mutable Lists Explained

This article compares Java's List.of() and Arrays.asList() methods, detailing their immutable versus mutable characteristics, handling of nulls, size constraints, and underlying array behavior, and provides practical code examples and guidance on choosing the appropriate method for different programming scenarios.

macrozheng
macrozheng
macrozheng
When to Use List.of vs Arrays.asList in Java: Immutable vs Mutable Lists Explained

Introduction

Java provides convenient ways to create lists, such as List.of and Arrays.asList. Although both can create collection objects easily, they have significant differences. This article explains the differences between List.of() and Arrays.asList(), their use cases, and when to choose each.

List.of()

List.of()

is a factory method introduced in Java 9 for creating an immutable list containing the specified elements. Key points:

Immutability: the list cannot be modified after creation.

Fixed size: adding or removing elements is not supported.

Null values: null elements are not allowed; attempting to add null throws NullPointerException.

Example:

List<String> immutable_list = List.of("apple", "banana", "orange");

Arrays.asList()

Arrays.asList()

has been available since early Java versions. It creates a fixed‑size list backed by the specified array. Its characteristics:

Mutability: the list allows element updates but not addition or removal.

Array‑backed: changes to the list affect the underlying array and vice versa.

Fixed size: attempts to add or remove elements throw UnsupportedOperationException.

Null values: unlike List.of(), null elements are permitted.

Example:

List<String> mutable_list = Arrays.asList("red", "green", "blue");

Application Scenarios

List.of()

Use when you need a fixed‑size, immutable collection. It guarantees collection integrity and prevents accidental modifications.

import java.util.List;

public class ListOfExample {
    public static void main(String[] args) {
        String[] colorsArray = { "Red", "Green", "Blue" };
        List<String> colors = List.of(colorsArray);
        colorsArray[0] = "Yellow";
        System.out.println(colors.get(0).equals(colorsArray[0])); // false
        System.out.println(colors.get(1).equals(colorsArray[1])); // true
        System.out.println(colors.get(2).equals(colorsArray[2])); // true
    }
}

Arrays.asList()

Use when you need a fixed‑size, array‑backed collection that can be modified, with changes reflected in the original array.

import java.util.Arrays;
import java.util.List;

public class ArraysAsListExample {
    public static void main(String[] args) {
        String[] colorsArray = { "Red", "Green", "Blue" };
        List<String> colors = Arrays.asList(colorsArray);
        colors.set(0, "Yellow");
        System.out.println(colors.get(0).equals(colorsArray[0])); // true
        System.out.println(colors.get(1).equals(colorsArray[1])); // true
        System.out.println(colors.get(2).equals(colorsArray[2])); // true
    }
}

Conclusion

Understanding the differences between List.of() and Arrays.asList() is crucial for Java developers. List.of() creates an immutable, fixed‑size list, while Arrays.asList() produces a mutable list backed by an array. By considering their characteristics and appropriate scenarios, you can select the right method for your specific programming needs.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaCollectionsArrays.asListImmutable ListList.ofMutable List
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.