@Test public void testPermutations() { logger.info("permutations"); Collection<List<String>> expResult = new ArrayList<>(); expResult.add(new ArrayList<>(Arrays.asList("a","b","c"))); expResult.add(new ArrayList<>(Arrays.asList("b","a","c"))); expResult.add(new ArrayList<>(Arrays.asList("b","c","a"))); expResult.add(new ArrayList<>(Arrays.asList("a","c","b"))); expResult.add(new ArrayList<>(Arrays.asList("c","a","b"))); expResult.add(new ArrayList<>(Arrays.asList("c","b","a"))); Collection<List<String>> result = Combinatorics.permutations(Arrays.asList("a","b","c")); assertEquals(expResult, result); }
public static <T> Collection<List<T>> permutations(Collection<T> elements) { Collection<List<T>> result = new ArrayList<>(); if (elements.isEmpty()) { result.add(new LinkedList<>()); return result; } List<T> rest = new LinkedList<>(elements); T head = rest.remove(0); for (List<T> permutations : permutations(rest)) { List<List<T>> subLists = new ArrayList<>(); for (int i = 0; i <= permutations.size(); i++) { List<T> subList = new ArrayList<>(); subList.addAll(permutations); subList.add(i, head); subLists.add(subList); } result.addAll(subLists); } return result; }
Combinatorics { public static <T> Collection<List<T>> permutations(Collection<T> elements) { Collection<List<T>> result = new ArrayList<>(); if (elements.isEmpty()) { result.add(new LinkedList<>()); return result; } List<T> rest = new LinkedList<>(elements); T head = rest.remove(0); for (List<T> permutations : permutations(rest)) { List<List<T>> subLists = new ArrayList<>(); for (int i = 0; i <= permutations.size(); i++) { List<T> subList = new ArrayList<>(); subList.addAll(permutations); subList.add(i, head); subLists.add(subList); } result.addAll(subLists); } return result; } }
Combinatorics { public static <T> Collection<List<T>> permutations(Collection<T> elements) { Collection<List<T>> result = new ArrayList<>(); if (elements.isEmpty()) { result.add(new LinkedList<>()); return result; } List<T> rest = new LinkedList<>(elements); T head = rest.remove(0); for (List<T> permutations : permutations(rest)) { List<List<T>> subLists = new ArrayList<>(); for (int i = 0; i <= permutations.size(); i++) { List<T> subList = new ArrayList<>(); subList.addAll(permutations); subList.add(i, head); subLists.add(subList); } result.addAll(subLists); } return result; }  }
Combinatorics { public static <T> Collection<List<T>> permutations(Collection<T> elements) { Collection<List<T>> result = new ArrayList<>(); if (elements.isEmpty()) { result.add(new LinkedList<>()); return result; } List<T> rest = new LinkedList<>(elements); T head = rest.remove(0); for (List<T> permutations : permutations(rest)) { List<List<T>> subLists = new ArrayList<>(); for (int i = 0; i <= permutations.size(); i++) { List<T> subList = new ArrayList<>(); subList.addAll(permutations); subList.add(i, head); subLists.add(subList); } result.addAll(subLists); } return result; }  static Collection<List<T>> permutations(Collection<T> elements); static Set<Set<T>> combinations(Set<T> elements, int subsetSize); static Stream<Set<T>> combinationsStream(Set<T> elements, int subsetSize); static Iterator<T[]> combinationsIterator(final T[] elements, final int subsetSize); }
Combinatorics { public static <T> Collection<List<T>> permutations(Collection<T> elements) { Collection<List<T>> result = new ArrayList<>(); if (elements.isEmpty()) { result.add(new LinkedList<>()); return result; } List<T> rest = new LinkedList<>(elements); T head = rest.remove(0); for (List<T> permutations : permutations(rest)) { List<List<T>> subLists = new ArrayList<>(); for (int i = 0; i <= permutations.size(); i++) { List<T> subList = new ArrayList<>(); subList.addAll(permutations); subList.add(i, head); subLists.add(subList); } result.addAll(subLists); } return result; }  static Collection<List<T>> permutations(Collection<T> elements); static Set<Set<T>> combinations(Set<T> elements, int subsetSize); static Stream<Set<T>> combinationsStream(Set<T> elements, int subsetSize); static Iterator<T[]> combinationsIterator(final T[] elements, final int subsetSize);  }