Let's say I have an array of Java 8 streams: Stream<T>[] streams
, I'd like to make a Stream where each element of the new stream is an array composed by picking one element from each of the initial base streams (let's assume they're all sequential).
For instance if I have:
streams [ 0 ] returning: ( "A", "B", "C" ),
streams [ 1 ] returning ( "X", "Y", "Z" )
and streams [ 2 ] as ( "0", "1", "2" )
I'd like a stream that returns
( { "A", "X", "0" }, { "B", "Y", "1" }, { "C", "Z", "2" } )
Is there some code that already implements this? I have an idea of how to do it, it would be a generalisation of the pair case, but I'd like to know if something reusable is already around.
EDIT: sorry, I realised I need some clarification:
I don't want to create the whole matrix, I want a stream that dynamically returns one row at a time (first A/X/0, then B/Y/1, etc), without having to occupy memory with all the rows in advance. I'm fine with reasonable assumptions over the sizes of base streams (eg, taking the minimum, stopping as soon as there is a stream that has no more elements to return).
I know this can be implemented by first turning the base streams into iterators, then creating a new iterator which of next() picks one element from each of the underlining iterators and returns a new row. That is what the pair example I've linked above does and I could implement it that way on myself, here I'm trying to understand if it has been already done in some library (I know JDK has no such function).