It’s possible, but I have a hard time imagining a situation where it would be worthwhile. We construct an iterator over the products, convert it to a spliterator and then to a stream.
int[] n = {1, 2, 3, 4, 5};
int[] m = {1, 2, 3, 4, 5};
IntStream x = Arrays.stream(n);
IntStream y = Arrays.stream(m);
PrimitiveIterator.OfInt nIt = x.iterator();
PrimitiveIterator.OfInt mIt = y.iterator();
Iterator<Integer> prodIterator = new Iterator<>() {
@Override
public boolean hasNext() {
return nIt.hasNext() && mIt.hasNext();
}
@Override
public Integer next() {
// Apply your BiFunction here
return nIt.next() * mIt.nextInt();
}
};
Iterable<Integer> prodIterable = () -> prodIterator;
Stream<Integer> productStream = StreamSupport.stream(prodIterable.spliterator(), false);
productStream.forEach(System.out::println);
Output:
1
4
9
16
25
If you want an IntStream
, I trust you to unbox the Stream<Integer>
yourself.
Notice that if the original streams have got different numbers of elements in them, extra elements in the longer stream will be tacitly ignored, which is probably not what you want. Please think of something better.