DEV Community

realNameHidden
realNameHidden

Posted on

3 1 1 1 1

List.of() vs Arrays.asList() in java


When working with Java, both List.of() (introduced in Java 9) and Arrays.asList() (available since Java 1.2) are commonly used to create lists, but they have subtle differences that can cause unexpected behavior and even crash your code if not understood properly. Let's explore these differences:

1. Mutability
List.of():
The list returned by List.of() is immutable, meaning you cannot modify it after creation. Any attempt to add, remove, or modify elements will throw an UnsupportedOperationException.
Example

List<String> list = List.of("apple", "banana", "orange");
list.add("grape"); // Throws UnsupportedOperationException

Enter fullscreen mode Exit fullscreen mode

Arrays.asList():
The list returned by Arrays.asList() is mutable but with limitations. You can modify elements in the list, but you cannot change the size (i.e., add or remove elements). This is because Arrays.asList() returns a fixed-size list backed by the array.
Example:

List<String> list = Arrays.asList("apple", "banana", "orange");
list.set(1, "grape"); // Works fine (modifying the element)
list.add("grape"); // Throws UnsupportedOperationException (size cannot change)

Enter fullscreen mode Exit fullscreen mode

2. Null Handling
List.of():
List.of() does not allow null values. If you try to create a list with null elements, it will throw a NullPointerException.
Example:

List<String> list = List.of("apple", null, "orange"); // Throws NullPointerException

Enter fullscreen mode Exit fullscreen mode

Arrays.asList():
Arrays.asList() allows null values and handles them just like any other element.

Example:

List<String> list = Arrays.asList("apple", null, "orange"); // No issues with null values

Enter fullscreen mode Exit fullscreen mode

3. Thread Safety
List.of():
Being immutable, the list returned by List.of() is inherently thread-safe since no modifications can occur.
Arrays.asList():
Arrays.asList() is not thread-safe because it's backed by an array, which can be modified in multiple threads without synchronization.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay