| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- package cn.com.goldenwater.dcproj.utils;
- import java.util.List;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
- public class ListUtil {
- private static final Integer DEFAULT_MAX_SEND = 1000;
- public static <T> List<List<T>> partition(List<T> list) {
- return partition(list, DEFAULT_MAX_SEND);
- }
- public static <T> List<List<T>> partition(List<T> list, int maxSend) {
- int limit = countStep(list.size(), maxSend);
- //方法一:使用流遍历操作
- // List<List<T>> mglist = new ArrayList<>();
- // Stream.iterate(0, n -> n + 1).limit(limit).forEach(i -> {
- // mglist.add(list.stream().skip(i * maxSend).limit(maxSend).collect(Collectors.toList()));
- // });
- //
- // return mglist;
- // 方法二:获取分割后的集合
- return Stream.iterate(0, n -> n + 1)
- .limit(limit)
- .parallel()
- .map(a -> list.stream()
- .skip(a * maxSend)
- .limit(maxSend)
- .parallel()
- .collect(Collectors.toList()))
- .collect(Collectors.toList());
- }
- /**
- * 计算切分次数
- */
- private static Integer countStep(Integer size, int maxSend) {
- return (size + maxSend - 1) / maxSend;
- }
- }
|