P27 { public static <T> List<List<List<T>>> group(List<T> list, List<Integer> gss) { if (gss.isEmpty()) { List<List<List<T>>> lists = new ArrayList<>(); lists.add(new ArrayList<>()); return lists; } int n = gss.get(0); List<Integer> ns = gss.subList(1, gss.size()); List<List<List<T>>> result = new ArrayList<>(); for (List<T> c : combinations(list, n)) { List<T> remaining = remaining(list, c); for (List<List<T>> cg : group(remaining, ns)) { List<List<T>> sg = new ArrayList<>(); sg.add(c); sg.addAll(cg); result.add(sg); } } return result; }  static List<List<List<T>>> group3(List<T> list); static List<List<List<T>>> group(List<T> list, List<Integer> gss);  }
@Test public void shouldGroupIntoThreeGroupsOfSize_2_2_and_5() throws Exception { List<String> input = Stream.of("aldo", "beat", "carla", "david", "evi", "flip", "gary", "hugo", "ida").collect(toList()); List<List<List<String>>> groups = P27.group(input, Stream.of(2, 2, 5).collect(Collectors.toList())); assertThat(groups, hasSize(756)); }
@Test public void shouldGroupIntoThreeGroupsOfSize_1_and_2_part2() throws Exception { List<String> input = Stream.of("a", "b", "c").collect(toList()); List<List<List<String>>> groups = P27.group(input, Stream.of(1, 2).collect(Collectors.toList())); assertThat(groups, hasSize(3)); }
@Test public void shouldGroupIntoThreeGroupsOfSize_2_3_and_4_part2() throws Exception { List<String> input = Stream.of("aldo", "beat", "carla", "david", "evi", "flip", "gary", "hugo", "ida").collect(toList()); List<List<List<String>>> groups = P27.group(input, Stream.of(2, 3, 4).collect(Collectors.toList())); assertThat(groups, hasSize(1260)); }