Java
Programming
Data Structures
List.of
Arrays.asList

What is the difference between List.of and Arrays.asList?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In Java, creating a quickly accessible list of elements can be performed using different utilities, namely List.of and Arrays.asList. Both functions provide a convenient way to generate a list from a set of elements but have different characteristics and use cases. Understanding the distinctions between these two can significantly impact code performance, mutability, and compatibility of the collections produced. Here we'll look at their definitions, differences, and typical usage scenarios.

Definitions and Overview

  • Array.asList: Introduced in Java 1.2, Arrays.asList(T... a) is a method in the Arrays utility class that returns a fixed-size list backed by the specified array. This allows developers to create a list view of the existing array. Any changes made to this list will directly affect the originally passed array, and vice-versa.
java
  List<String> list = Arrays.asList("apple", "banana", "cherry");
  list.set(1, "date"); // Changes the underlying array element at index 1
  • List.of: This is a relatively newer method, introduced in Java 9, as part of immutable collections enhancements. The method List.of(E... elements) returns an immutable list containing the specified elements. Once this list is created, it cannot be modified - no additions, deletions, or changes to elements are allowed.
java
  List<String> immutableList = List.of("apple", "banana", "cherry");
  // immutableList.add("date"); // Throws UnsupportedOperationException

Key Differences

  1. Mutability:
    • Arrays.asList returns a mutable list. However, it is "fixed-size", meaning elements can be replaced, but no new elements can be added or removed.
    • List.of generates an immutable list. No modifications can be made to this list after its creation.
  2. Underlying Data Structure:
    • The list returned by Arrays.asList() is backed by an array, so changes to the list or the array reflect in both.
    • List.of() creates an independent, immutable list that is not backed by a visible array, and hence cannot be modified.
  3. Performance Considerations:
    • Arrays.asList is useful when you want to quickly view array content as a list without creating a new data structure. It offers fast read access but limited write operations.
    • List.of is highly suited for creating truly immutable lists for safe sharing in concurrent environments without synchronization overhead.
  4. Null Elements:
    • Arrays.asList allows null elements.
    • List.of does not allow null elements and will throw NullPointerException if an attempt is made to create a list with nulls.
  5. Method Overloading:
    • Arrays.asList is overloaded to support different primitive arrays and Object arrays.
    • List.of is variably overloaded to support up to 10 elements explicitly and an array version for more elements, but always wraps them into a list of objects.

Practical Usage

  • Use Arrays.asList when you need a modifiable view of an array.
  • Opt for List.of when creating a list of known elements that shouldn’t change, or for safe concurrent access.

Summary Table

FeatureArrays.asListList.of
MutabilityMutable (fixed size)Immutable
Null elements allowed?YesNo
PerformanceFast for array-backed operationsOptimal for immutable list operations
Introduced inJava 1.2Java 9
Ideal use caseQuick no-copy access to arrayCreating immutable list for safe sharing

Conclusion

Choosing between Arrays.asList and List.of depends largely on the application requirements — whether you need immutability and thread-safety or flexibility of modifying the list that mirrors an array. For modern applications, especially those involving concurrent data access, using List.of might offer more advantages due to its immutability and inherent thread-safety, while Arrays.asList remains useful for internal or controlled environments where array and list need to be manipulated in tandem.


Course illustration
Course illustration

All Rights Reserved.