Transform a list of lists into a list using Lambdas

advertisements

If I have a List<List<Object>> can I turn it into a List<Object> that contains all the objects using continuous (not breaking the invocation sequence) invocations of lambdas?


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());