I have a list and a batch size. I need to split it into a list of lists where each list has a maximum size of batch size preferably using java8 Stream API.
One important condition is, if I have an empty list []
, I need to get [[]]
.
I am doing this
final AtomicInteger counter = new AtomicInteger();
List<List<Integer>> result = listToSplit.stream()
.collect(Collectors.groupingBy(it -> counter.getAndIncrement() / batchSize))
.values();
But for empty list, it gives [[null]]
>` I can't reproduce "But for empty list, it gives [[null]]". For empty list I am getting `[]` not `[null]`.