☕ Java Intermediate

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

Answer

Arrays.asList() returns a fixed-size list backed by the original array — you can update elements (set), but cannot add or remove (throws UnsupportedOperationException). Changes to the list affect the original array and vice versa. It allows null elements. List.of() (Java 9+) returns a truly immutable list — any attempt to add, remove, or set throws UnsupportedOperationException. It does not permit null elements (throws NullPointerException). List.of() is preferred for creating small, permanent collections. new ArrayList<>(List.of(...)) creates a mutable copy. Collections.unmodifiableList() wraps any list in an unmodifiable view but the backing list can still be changed.