How to run nested collection on java 8 thread

I have a list of objects A, A has a property called Address, which has a street name - streetName

From the list of objects A I want to get a list of all street names. One level assembly seems quite feasible from threads, but how can I get a nested string using a single line of code.

So, to get the address list from object A, I can do this:

listOfObjectsA.stream().map(a::getAddress).collect(Collectors.toList()); 

My ultimate goal is to get a list of street names, so I cannot find a second level collection using lambda.

I could not find the exact example I was looking for. Can someone please help me with this.

+6
source share
1 answer

You can simply bind another map operation to get street names:

 listOfObjectsA .stream() .map(a::getAddress) .map(a -> a.getStreetName()) // or a::getStreetName .collect(Collectors.toList()); 

The first map converts your objects into Address objects, the next map takes those Address objects and converts them into street names , which are then collected by the collector.

Flow operations form a pipeline, so you can have as many operations as you need before terminal operations (in this case, the collect operation).

+6
source

All Articles