Reputation: 22291
I need to create two objects based on a single object to stream and process further. How can I achieve this using streams?
The pseudocode could look like this:
stream.stream().
map(p -> new Object(p.getParam1()) <AND> new Object(p.getParam2()) ).
collect(Collectors.toList());
Upvotes: 2
Views: 917
Reputation: 394126
There's no need to use both map
and flatMap
.
flatMap
by itself will do :
stream.stream()
.flatMap(p -> Stream.of(new Object(p.getParam1()), new Object(p.getParam2())))
.collect(Collectors.toList());
Upvotes: 6