Video in progress

How do the lists returned by List.of() and Arrays.asList() differ?

List.of gives a fully immutable list and does not accept null. Arrays.asList is a fixed-size "view" over an array: set works, add/remove don't, and if the array changes, the list changes too.

📖 Answer

String[] arr = {"a", "b"};
List<String> view = Arrays.asList(arr);
view.set(0, "x");          // works, and arr[0] is now "x" too
view.add("c");             // UnsupportedOperationException

List<String> fixed = List.of("a", "b");
fixed.set(0, "x");         // UnsupportedOperationException
List.of("a", null);        // NullPointerException
fixed.contains(null);      // this is an NPE too!

A short table:

Arrays.asList List.of
set yes no
add/remove no no
null element allowed not allowed
backed by the array yes no

If you need an ordinary mutable list, wrap either of them in new ArrayList<>(...).