Returns every possible list that can be formed by choosing one element
from each of the given sets in order; the "n-ary
Cartesian
product" of the sets. For example:
Sets.cartesianProduct(ImmutableList.of(
returns a set containing six lists:
-
ImmutableList.of(1, "A")
-
ImmutableList.of(1, "B")
-
ImmutableList.of(1, "C")
-
ImmutableList.of(2, "A")
-
ImmutableList.of(2, "B")
-
ImmutableList.of(2, "C")
The result is guaranteed to be in the "traditional", lexicographical
order for Cartesian products that you would get from nesting for loops:
for (B b0 : sets.get(0)) }}
Note that if any input set is empty, the Cartesian product will also be
empty. If no sets at all are provided (an empty list), the resulting
Cartesian product has one element, an empty list (counter-intuitive, but
mathematically consistent).
Performance notes: while the cartesian product of sets of size
m, n, p is a set of size
m x n x p, its actual memory
consumption is much smaller. When the cartesian set is constructed, the
input sets are merely copied. Only as the resulting set is iterated are the
individual lists created, and these are not retained after iteration.