/**
* 将原始list按照MAX_LIST_SIZE分组
* @param list
* @return
*/
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;
}
Guava中提供了类似功能的实现
List<Integer> numList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
List<List<Integer>> partList = Lists.partition(numList, 3);