com.es.plus.adapter.util.CollectionUtil Maven / Gradle / Ivy
package com.es.plus.adapter.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* 集合工具
*
* @author hzh
* @date 2022/12/28
*/
public class CollectionUtil {
public static List> splitList(Collection collection, int splitSize) {
if (collection.isEmpty()) {
return Collections.emptyList();
}
//分割成几个集合
int splitCount = (collection.size() + splitSize - 1) / splitSize;
//从0开始每次循环n+1 只截取指定长度的流集合。
return Stream.iterate(0, n -> n + 1)
.limit(splitCount)
//并行
.parallel()
.map(index -> collection.stream().skip((long)index * splitSize).limit(splitSize).collect(Collectors.toList()))
.filter(s -> !s.isEmpty())
.collect(Collectors.toList());
}
public static void main(String[] args) {
ArrayList objects = new ArrayList<>();
objects.add("11");
objects.add("11222");
List> collections = splitList(objects, 1);
List collect = collections.stream().map(a -> (ArrayList) a)
.flatMap(Collection::stream).collect(Collectors.toList());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy