@Test public void testGroupPartitions() { List<List<Integer>> grouped = ConnectorUtils.groupPartitions(FIVE_ELEMENTS, 1); assertEquals(Arrays.asList(FIVE_ELEMENTS), grouped); grouped = ConnectorUtils.groupPartitions(FIVE_ELEMENTS, 2); assertEquals(Arrays.asList(Arrays.asList(1, 2, 3), Arrays.asList(4, 5)), grouped); grouped = ConnectorUtils.groupPartitions(FIVE_ELEMENTS, 3); assertEquals(Arrays.asList(Arrays.asList(1, 2), Arrays.asList(3, 4), Arrays.asList(5)), grouped); grouped = ConnectorUtils.groupPartitions(FIVE_ELEMENTS, 5); assertEquals(Arrays.asList(Arrays.asList(1), Arrays.asList(2), Arrays.asList(3), Arrays.asList(4), Arrays.asList(5)), grouped); grouped = ConnectorUtils.groupPartitions(FIVE_ELEMENTS, 7); assertEquals(Arrays.asList(Arrays.asList(1), Arrays.asList(2), Arrays.asList(3), Arrays.asList(4), Arrays.asList(5), Collections.EMPTY_LIST, Collections.EMPTY_LIST), grouped); }
public static <T> List<List<T>> groupPartitions(List<T> elements, int numGroups) { if (numGroups <= 0) throw new IllegalArgumentException("Number of groups must be positive."); List<List<T>> result = new ArrayList<>(numGroups); int perGroup = elements.size() / numGroups; int leftover = elements.size() - (numGroups * perGroup); int assigned = 0; for (int group = 0; group < numGroups; group++) { int numThisGroup = group < leftover ? perGroup + 1 : perGroup; List<T> groupList = new ArrayList<>(numThisGroup); for (int i = 0; i < numThisGroup; i++) { groupList.add(elements.get(assigned)); assigned++; } result.add(groupList); } return result; }
ConnectorUtils { public static <T> List<List<T>> groupPartitions(List<T> elements, int numGroups) { if (numGroups <= 0) throw new IllegalArgumentException("Number of groups must be positive."); List<List<T>> result = new ArrayList<>(numGroups); int perGroup = elements.size() / numGroups; int leftover = elements.size() - (numGroups * perGroup); int assigned = 0; for (int group = 0; group < numGroups; group++) { int numThisGroup = group < leftover ? perGroup + 1 : perGroup; List<T> groupList = new ArrayList<>(numThisGroup); for (int i = 0; i < numThisGroup; i++) { groupList.add(elements.get(assigned)); assigned++; } result.add(groupList); } return result; } }
ConnectorUtils { public static <T> List<List<T>> groupPartitions(List<T> elements, int numGroups) { if (numGroups <= 0) throw new IllegalArgumentException("Number of groups must be positive."); List<List<T>> result = new ArrayList<>(numGroups); int perGroup = elements.size() / numGroups; int leftover = elements.size() - (numGroups * perGroup); int assigned = 0; for (int group = 0; group < numGroups; group++) { int numThisGroup = group < leftover ? perGroup + 1 : perGroup; List<T> groupList = new ArrayList<>(numThisGroup); for (int i = 0; i < numThisGroup; i++) { groupList.add(elements.get(assigned)); assigned++; } result.add(groupList); } return result; }  }
ConnectorUtils { public static <T> List<List<T>> groupPartitions(List<T> elements, int numGroups) { if (numGroups <= 0) throw new IllegalArgumentException("Number of groups must be positive."); List<List<T>> result = new ArrayList<>(numGroups); int perGroup = elements.size() / numGroups; int leftover = elements.size() - (numGroups * perGroup); int assigned = 0; for (int group = 0; group < numGroups; group++) { int numThisGroup = group < leftover ? perGroup + 1 : perGroup; List<T> groupList = new ArrayList<>(numThisGroup); for (int i = 0; i < numThisGroup; i++) { groupList.add(elements.get(assigned)); assigned++; } result.add(groupList); } return result; }  static List<List<T>> groupPartitions(List<T> elements, int numGroups); }
ConnectorUtils { public static <T> List<List<T>> groupPartitions(List<T> elements, int numGroups) { if (numGroups <= 0) throw new IllegalArgumentException("Number of groups must be positive."); List<List<T>> result = new ArrayList<>(numGroups); int perGroup = elements.size() / numGroups; int leftover = elements.size() - (numGroups * perGroup); int assigned = 0; for (int group = 0; group < numGroups; group++) { int numThisGroup = group < leftover ? perGroup + 1 : perGroup; List<T> groupList = new ArrayList<>(numThisGroup); for (int i = 0; i < numThisGroup; i++) { groupList.add(elements.get(assigned)); assigned++; } result.add(groupList); } return result; }  static List<List<T>> groupPartitions(List<T> elements, int numGroups);  }