Quantcast
Channel: Initialization of an ArrayList in one line - Stack Overflow
Viewing all articles
Browse latest Browse all 37

Answer by Mark for Initialization of an ArrayList in one line

$
0
0

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<String> places = Stream.of("Buenos Aires", "Córdoba", "La Plata").collect(Collectors.toList());

If you need to ensure that your List is an ArrayList:

ArrayList<String> places = Stream.of("Buenos Aires", "Córdoba", "La Plata").collect(Collectors.toCollection(ArrayList::new));

Viewing all articles
Browse latest Browse all 37

Trending Articles