P27 { public static <T> List<List<List<T>>> group3(List<T> list) { List<List<List<T>>> result = new ArrayList<>(); for (List<T> combinationOf2 : combinations(list, 2)) { List<T> r = remaining(list, combinationOf2); for (List<T> combinationOf3 : combinations(r, 3)) { result.add(Stream.of(combinationOf2, combinationOf3, remaining(r, combinationOf3)).collect(toList())); } } 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_3_and_4() throws Exception { List<String> input = Stream.of("aldo", "beat", "carla", "david", "evi", "flip", "gary", "hugo", "ida").collect(toList()); List<List<List<String>>> groups = P27.group3(input); assertThat(groups, hasSize(1260)); }