Java Cartesian product of lists
How can I implement a function using Java 8 to take some number of streams, and produce a stream in which each element is a list consisting of one member of the Cartesian product of the streams? I've looked at this question -- that question uses an aggregator that is a BinaryOperator (taking two items of like type and producing an item of the same type). I'd like the items in the end result to be Lists rather than the types of the elements in the input streams. Concretely, supposing my desired function is called product, the following: Stream
should print: [A, I, Y] [A, I, Z] [A, J, Y] [A, J, Z] [A, K, Y] [A, K, Z] [B, I, Y] ... [D, K, Y] [D, K, Z]Ideally, I'd like for this operation to be as lazy as possible. For example, if the input streams are produced by Stream.generate(), it'd be great if the suppliers of those streams weren't executed until absolutely needed. A possible solution is as follows: private static
The product-call returns a Stream Let A and B be two sets, Cartesian productA × B is the set of all ordered pair of elements from A and B
Output : Practical Examples: 1) A set of playing cards is Cartesian product of a four element set to a set of 13 elements.2) A two dimensional coordinate system is a Cartesian product of two sets of real numbers. Reference: https://en.wikipedia.org/wiki/Cartesian_product |