2015-01-07 3 views
7

Ich versuche, Liste eines Streams zu contrastieren und zu verarbeiten.Java8 Lambda: concat Liste

class A { 
    public List<B> bList; 
} 
List<A> aList; 
aList.stream().map(a -> a.bList).... 

Hier bekomme ich mehrere Liste von b.

Aber ich möchte alle meine b in nur einer Liste sammeln. Irgendwelche Ideen ?

Antwort

15

Das ist, was flatMap ist für:

List<B> bList = aList.stream() 
        .flatMap(a -> a.bList.stream()) 
        .collect(Collectors.toList());