场景:当 List 集合数据量比较大时,需要对集合分组,分批处理数据。
代码:
public static void main(String[] args) {// 集合List<String> list = new ArrayList<>();for (int i = 0; i < 100; i++) {list.add(i + "");}// 分组长度int groupSize = 10;int size = list.size();int count = size % groupSize == 0 ? size / groupSize : size / groupSize + 1;for (int i = 0; i < count; i++) {int fromIndex = i * groupSize;int toIndex = (i + 1) * groupSize > size ? size : (i + 1) * groupSize;List<String> subList = list.subList(fromIndex, toIndex);System.out.println(subList);}}
作者:殷建卫 链接:https://www.yuque.com/yinjianwei/vyrvkf/gxwu61 来源:殷建卫 - 架构笔记 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
