How can I turn a List of Lists into a List in Java 8?

The Solution to How can I turn a List of Lists into a List in Java 8? is


You can use flatMap to flatten the internal lists (after converting them to Streams) into a single Stream, and then collect the result into a list:

List<List<Object>> list = ...
List<Object> flat = 
    list.stream()
        .flatMap(List::stream)
        .collect(Collectors.toList());

~ Answered on 2014-08-05 19:50:04


Most Viewed Questions: