Returns every possible list that can be formed by choosing one element
from each of the given lists in order; the "n-ary
Cartesian
product" of the lists. For example:
Lists.cartesianProduct(ImmutableList.of(
returns a list containing six lists in the following order:
-
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 : lists.get(0)) }}
Note that if any input list is empty, the Cartesian product will also be
empty. If no lists 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 lists of size
m, n, p is a list of size
m x n x p, its actual memory
consumption is much smaller. When the cartesian product is constructed, the
input lists are merely copied. Only as the resulting list is iterated are
the individual lists created, and these are not retained after iteration.