1

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.

zck
  • 2,712
  • 2
  • 25
  • 44
  • Stream s1 = Stream.of(1,2,3); Stream s2 = Stream.of(10,20,30); Iterator i1 = s1.iterator(); Iterator i2 = s2.iterator(); Stream s = Stream.generate(()-> i1.next()+i2.next()).limit(3); s.forEach(System.out::println); – Jean-Baptiste Yunès Mar 02 '18 at 17:11

0 Answers0