I have two streams, say:
final Stream<Integer> firstStream = Stream.of(1, 2, 3);
final Stream<Integer> secondStream = Stream.of(10, 20, 30);
I want to combine the streams by adding the pairwise elements, as if I wrote
Streams.map(
(item1, item2) -> item1 + item2,
firstStream,
secondStream);
And get as the result a stream equivalent to:
Streams.of(11, 22, 33);
Is there a built-in method to do this? I could roll my own with a while
or for
loop, but I'm using Java 8 and would prefer to use something simpler.
Note that the process I want to do is not simply adding the elements, but is a more complex function. But this should be a good example of what I want to do.