package com.cmbchina.face.common.util;
import com.google.common.collect.Lists;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
/***
*
* 拆分List工具类
* 将一个list 拆分成一个大的list 里面有很多的小list
* @Author LH!
* @Parms resList 原List
* @Parms subListLength 每一个子集合长度
* return 分割之后的合集
* */
public class SpitListUtil {
public static <T> List<List<T>> spit(List<T> resList, int subListLength) {
if (CollectionUtils.isEmpty(resList) || subListLength == 0) {
return Lists.newArrayList();
}
List<List<T>> ret = Lists.newArrayList();
int size = resList.size(); //原来List 总长度
if (size <= subListLength) {
//指定长度短于原始长度时
ret.add(resList);
} else {
int pre = size / subListLength; //可以分成几页
int last = size % subListLength;
if (last != 0) { //有余数 页数+1
pre = pre + 1;
}
int currentBeginIndex = 1; //当前开头下标
for (int i = 1; i <= pre; i++) { //循环每页
int overIndex = Math.min(i * subListLength, size); //当前的结束下标
List<T> branchList = Lists.newArrayList();
for (int k = currentBeginIndex; k <= overIndex; k++) { //当前区间段
branchList.add(resList.get(k - 1));
}
currentBeginIndex = overIndex + 1; //更新起始位置下标
ret.add(branchList);
}
}
return ret;
}
}
java List分割工具类
最新推荐文章于 2024-05-11 23:39:29 发布