一、有一个list,对list中的数据进行分组
代码语言:javascript复制List<List> curriculumIdSplitList = SplitListUtils.splitList(curriculumIds, 20);
for (List<String> curriculumIdSplit : curriculumIdSplitList) {
}
工具类SplitListUtils
代码语言:javascript复制package com.speiyou.teacher.data.common;
import java.util.ArrayList;
import java.util.List;
public class SplitListUtils {
/**
* 切分list
* @param sourceList
* @param groupSize 每组定长
* @return
*/
public static List<List> splitList(List sourceList, int groupSize) {
int length = sourceList.size();
// 计算可以分成多少组
int num = (length groupSize - 1) / groupSize;
List<List> newList = new ArrayList<>(num);
for (int i = 0; i < num; i ) {
// 开始位置
int fromIndex = i * groupSize;
// 结束位置
int toIndex = (i 1) * groupSize < length ? (i 1) * groupSize : length;
newList.add(sourceList.subList(fromIndex, toIndex));
}
return newList;
}
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("1");
List<List> lists = splitList(list, 2);
System.out.println("list:" list.toString());
System.out.println(lists);
}
}