Dariusz
Dariusz

Reputation: 22291

Stream create two objects from one and process further

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

Answers (1)

Eran
Eran

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

Related Questions