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.
Introduction
Java provides convenient ways to create lists, such as
List.ofand
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:
<code>List<String> immutable_list = List.of("apple", "banana", "orange");</code>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:
<code>List<String> mutable_list = Arrays.asList("red", "green", "blue");</code>Application Scenarios
List.of()
Use when you need a fixed‑size, immutable collection. It guarantees collection integrity and prevents accidental modifications.
<code>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
}
}
</code>Arrays.asList()
Use when you need a fixed‑size, array‑backed collection that can be modified, with changes reflected in the original array.
<code>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
}
}
</code>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.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.