I have this split method inside which I have a loop. This loop is running 4 times but it should run 5 times. Any idea why is it behaving like this?
public static <T> List<List<T>> split(List<T> bigCollection, int maxBatchSize) {
List<List<T>> result = new ArrayList<List<T>>();
if (CollectionUtils.isEmpty(bigCollection)) {
// return empty list
} else if (bigCollection.size() < maxBatchSize) {
result.add(bigCollection);
} else {
for (int i = 0; (i + maxBatchSize) <= bigCollection.size(); i = i + maxBatchSize) {
result.add(bigCollection.subList(i, i + maxBatchSize));
}
if (bigCollection.size() % maxBatchSize > 0) {
result.add(bigCollection.subList((int) (bigCollection.size() / maxBatchSize) * maxBatchSize,
bigCollection.size()));
}
}
return result;
}
public static void main(String[] args) {
List<String> coll = new ArrayList<String>();
coll.add("1");
coll.add("2");
coll.add("3");
coll.add("4");
coll.add("5");
coll.add("6");
coll.add("7");
coll.add("8");
System.out.println(split(coll, 2));
}
Output - [[1, 2], [3, 4], [5, 6], [7, 8]]
According to me this code should break when loop runs the fifth time and it tries to perform sublist function.
(i + maxBatchSize) <= bigCollection.size()check i for 2,4,6,8 and in the fifth run for 10 agains 8 and then stops. Whats wrong ?