Generate all combinations from multiple lists
This operation is called cartesian product. Guava provides an utility function for that: Lists.cartesianProduct
You need recursion:
Let's say all your lists are in lists
, which is a list of lists. Let result
be the list of your required permutations. You could implement it like this:
void generatePermutations(List<List<Character>> lists, List<String> result, int depth, String current) {
if (depth == lists.size()) {
result.add(current);
return;
}
for (int i = 0; i < lists.get(depth).size(); i++) {
generatePermutations(lists, result, depth + 1, current + lists.get(depth).get(i));
}
}
The ultimate call will be like this:
generatePermutations(lists, result, 0, "");