16/10/2024
List.of and Arrays.asList are both used to create lists in Java, but they have some differences:
1. List.of()
Introduced in Java 9.
Creates an immutable list.
Does not allow null elements.
Returns a list with a fixed size.
2. Arrays.asList()
Available since Java 1.2.
Creates a mutable list backed by an array.
Allows null elements.
The size of the list is fixed, but the elements can be changed.
Ref: https://websparrow.org/java/arrays-aslist-vs-list-of-in-java
List.of and Arrays.asList are both used to create lists in Java, but they have some differences.