HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavaCritical

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

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howjavalistslistcanturninto

Problem

If I have a List>, how can I turn that into a List that contains all the objects in the same iteration order by using the features of Java 8?

Solution

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 = ...
List flat = 
    list.stream()
        .flatMap(List::stream)
        .collect(Collectors.toList());

Code Snippets

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

Context

Stack Overflow Q#25147094, score: 1528

Revisions (0)

No revisions yet.