I wanted to create a list of options for testing purposes. At first, I did this:
ArrayList places = new ArrayList();
places.add(\
Collection literals didn't make it into Java 8, but it is possible to use the Stream API to initialize a list in one rather long line:
List places = Stream.of("Buenos Aires", "Córdoba", "La Plata").collect(Collectors.toList());
If you need to ensure that your List
is an ArrayList
:
ArrayList places = Stream.of("Buenos Aires", "Córdoba", "La Plata").collect(Collectors.toCollection(ArrayList::new));