Split the list into "maximum" chunks of equal size
I have a list of unrated items and I want them to be divided into 5 folds. Currently I have done this as I am just trying to split evenly into chunks and the latter is the so called "extended" chunk and this is causing problems for me.
When I have nonRatedItems = {1,2,3,4,5,6,7,8}
, then the pieces are:
1
2
3
4
5 6 7 8
I want to avoid this so that the output is:
1 6
2 7
3 8
4
5
Current source:
Collections.shuffle(nonRatedItems);
nonRatedItems = nonRatedItems.subList(0, (int) Math.ceil(nonRatedItems.size() * 0.2));
int tfoldSize = nonRatedItems.size() / 5;
List<List<Integer>> negativeTestFolds = new ArrayList<List<Integer>>();
if (tfoldSize < 1) {
nonRatedItems.stream().map(Lists::newArrayList).collect(Collectors.toCollection(() -> negativeTestFolds));
} else {
for (int i = 0; i < 5; i++) {
int endIndex = i < 4 ? (i + 1) * tfoldSize : nonRatedItems.size();
negativeTestFolds.add(nonRatedItems.subList(i * tfoldSize, endIndex));
}
}
Any suggestion / help is appreciated
+3
source to share
1 answer