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.
- 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.
Key Differences
- Mutability:
Arrays.asListreturns a mutable list. However, it is "fixed-size", meaning elements can be replaced, but no new elements can be added or removed.List.ofgenerates an immutable list. No modifications can be made to this list after its creation.
- 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.
- Performance Considerations:
Arrays.asListis 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.ofis highly suited for creating truly immutable lists for safe sharing in concurrent environments without synchronization overhead.
- Null Elements:
Arrays.asListallows null elements.List.ofdoes not allow null elements and will throwNullPointerExceptionif an attempt is made to create a list with nulls.
- Method Overloading:
Arrays.asListis overloaded to support different primitive arrays and Object arrays.List.ofis 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.asListwhen you need a modifiable view of an array. - Opt for
List.ofwhen creating a list of known elements that shouldn’t change, or for safe concurrent access.
Summary Table
| Feature | Arrays.asList | List.of |
| Mutability | Mutable (fixed size) | Immutable |
| Null elements allowed? | Yes | No |
| Performance | Fast for array-backed operations | Optimal for immutable list operations |
| Introduced in | Java 1.2 | Java 9 |
| Ideal use case | Quick no-copy access to array | Creating 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.

