List拆分成指定大小的subList

@Slf4j
public class ListUtil {

	// 拆分
	public static  List> getSubList(List list, int subSize) {

		if (CollectionUtils.isEmpty(list)) {
			return Lists.newArrayListWithCapacity(0);
		}
		if (subSize <= 0) {
			return (List>) Lists.newArrayList(list);
		}
		List> resultList = Lists.newArrayList();
		int loopCount = 0;
		if (MathUtils.modulo(list.size(), subSize).intValue() == 0) {
			loopCount = MathUtils.divide(list.size(), subSize).intValue();
		} else {
			loopCount = MathUtils.divide(list.size(), subSize).intValue() + 1;
		}
		for (int i = 0; i < loopCount; i++) {
			int start = MathUtils.multiply(i, subSize).intValue();
			int end = MathUtils.add(start, subSize).intValue();

			if (end > list.size()) {
				end = list.size();
			}
			// subList方法返回的仍然是原列表, 创建新的子列表防止返回的子列表修改污染了原来列表
			resultList.add(Lists.newArrayList(list.subList(start, end)));
		}
		return resultList;
	}
}


你可能感兴趣的:(List拆分成指定大小的subList)