/** * @param taskList 集合list * @param groupSize 每一组大小 */ public static List<List<T>> splitList(List<T> taskList, int groupSize) { if (taskList == null || taskList.size() == 0 || groupSize <= 0) { throw new IllegalArgumentException("Input args error."); } List<List<T>> results = new ArrayList<>(); int taskSize = taskList.size(); if (groupSize >= taskSize) { results.add(taskList); return results; } int fromIndex = 0; int toIndex = 0; int number = (taskSize + groupSize - 1) / groupSize; for (int i = 0; i < number; i++) { fromIndex = i * groupSize; toIndex = Math.min((i + 1) * groupSize, taskSize); List<T> subList = taskList.subList(fromIndex, toIndex); results.add(subList); } return results; } /** * 将 sources 等分成 count 份 * * @param sources sourceList * @param listCount count number of copies * @return all sublist */ public static List<List<T>> averageAssign(List<T> sources, int listCount) { if (sources == null || sources.isEmpty() || listCount <= 0) { throw new IllegalArgumentException("Input args error."); } List<List<T>> results = new ArrayList<>(); int size = sources.size(); if (size <= listCount) { results.add(sources); return results; } int remainder = size % listCount; int number = size / listCount; int offset = 0; for (int i = 0; i < listCount; i++) { List<T> values; if (remainder > 0) { values = sources.subList(i * number + offset, (i + 1) * number + offset + 1); remainder--; offset++; } else { values = sources.subList(i * number + offset, (i + 1) * number + offset); } results.add(values); } return results; }
将list数据分组/等分
最新推荐文章于 2024-06-19 23:26:41 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)