Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Adapted from https://stackoverflow.com/a/23870892/5602641
- public static <T> Collection<List<T>> permutations(List<Collection<T>> collections) {
- if (collections == null || collections.isEmpty()) {
- return Collections.emptyList();
- } else {
- Collection<List<T>> output = new LinkedList<T>();
- recursePermutations(collections, output, 0, new LinkedList<T>());
- return output;
- }
- }
- private static <T> void recursePermutations(List<Collection<T>> source, Collection<List<T>> output, int depth, List<T> current) {
- if (depth == source.size()) {
- output.add(current);
- return;
- }
- Collection<T> currentCollection = source.get(depth);
- for (T element : currentCollection) {
- List<T> copy = new LinkedList<T>(current);
- copy.add(element);
- recursePermutations(source, output, depth+1, copy);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement