java循环数组列表的批处理
我想在小批量中迭代ArrayList
例如,如果ArrayList大小为75,批处理大小为10,我希望它处理记录0-10,然后10-20,然后20-30,等等
我试过这个,但不起作用:
int batchSize = 10;
int start = 0;
int end = batchSize;
for(int counter = start ; counter < end ; counter ++)
{
if (start > list.size())
{
System.out.println("breaking");
break;
}
System.out.println("counter " + counter);
start = start + batchSize;
end = end + batchSize;
}
# 1 楼答案
您需要的是:Lists.partition(java.util.List, int)来自Google Guava
例如:
# 2 楼答案
# 3 楼答案
您可以像从批次大小和列表大小中提取余数一样查找计数