Java List按指定size分批
public static <E> List<List<E>> generateListGroup(List<E> list, int maxListSize) {
List<List<E>> listGroup = Lists.newArrayList();
if (CollectionUtils.isEmpty(list) || list.size() <= maxListSize) {
listGroup.add(list);
} else {
int pages = (int) Math.ceil((double)list.size() / (double)maxListSize);
for (int i = 0; i < pages; i++) {
int fromIndex = i * maxListSize;
int toIndex = (i + 1) * maxListSize > list.size() ? list.size()
: (i + 1) * maxListSize;
List<E> subList = list.subList(fromIndex, toIndex);
listGroup.add(subList);
}
}
return listGroup;
}
执行后的效果
原博客地址: https://www.iteye.com/blog/umgsai-2410340.