Java 9 has the following method to create an immutable list as documented (or documented as unmodifiableJava 10+):
List<String> places = List.of("Buenos Aires", "Córdoba", "La Plata");
which is easily adapted to create a mutable list, if required:
List<String> places = new ArrayList<>(List.of("Buenos Aires", "Córdoba", "La Plata"));
Similar methods are available for Set
and Map
.
Please note that these methods do not accept null
elements, a little bit hidden in the documentation:
Throws:
NullPointerException
- if an element isnull
...