1.java8 Stream 大数据量List分批处理
/**
* 将一个集合进行分割
*
* @param collections 需要分割的集合 (List LinkedHashSet TreeSet LinkedList)
* @param maxNum 集合最大的数量
* @return 分割后的集合
*/
public static <T> Collection<Collection<T>> subCollection(Collection<T> collections, long maxNum) {
//计算切分次数 long limit = (collections.size() + maxNum - 1) / maxNum;
return Stream.iterate(0, n -> n + 1).limit(limit).parallel().map(
a -> collections.stream().skip(a * maxNum).limit(maxNum).parallel().collect(Collectors.toList())
).collect(Collectors.toList());
}
2.使用google guava对List进行分割
List<User> users = userService.findAll();
//按每50个一组分割
List<List<User>> parts = Lists.partition(users, 50);
3.使用apache common collection
List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
List<List<Integer>> subs = ListUtils.partition(intList, 3);