How can I initialize an `ArrayList` in one line in Java?

You can use List.of() if you don’t need a mutable list. This is the cleanest and most efficient option when you only need to read from the list.

List<String> places = List.of("Buenos Aires", "Córdoba", "La Plata");

:white_check_mark: Why?

  • Immutable: This ensures no accidental modifications.
  • More performant than Arrays.asList() (since List.of() doesn’t create a fixed-size array). :x: But: It’s immutable—no add() or remove() after initialization.