1.Lists.partition方法
引入Maven依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>
方法:
List list=new ArrayList();
//每100个元素分为一个新的List
List<List<Integer>> parts = Lists.partition(list, 100);
方法二:Stream流实现分割List
/**
*
* @param list 需要分割的集合
* @param divideSize 每个List中含几个元素
* @return
*/
public static List<Object> divideArrays(List list,int divideSize){
Integer splitCount=(list.size()+divideSize-1)/divideSize;
List<Object> collect = Stream.iterate(0, n -> n + 1)
.limit(splitCount)
.parallel()
.map(a -> list.parallelStream().skip(a * divideSize).limit(divideSize).collect(Collectors.toList()))
.filter(b -> !list.isEmpty())
.collect(Collectors.toList());
return collect;
}