如何将一个数组列表(size=1000)拆分为多个相同大小(=10)的数组列表?
ArrayList<Integer> results;
如何将一个数组列表(size=1000)拆分为多个相同大小(=10)的数组列表?
ArrayList<Integer> results;
当前回答
import org.apache.commons.collections4.ListUtils;
ArrayList<Integer> mainList = .............;
List<List<Integer>> multipleLists = ListUtils.partition(mainList,100);
int i=1;
for (List<Integer> indexedList : multipleLists){
System.out.println("Values in List "+i);
for (Integer value : indexedList)
System.out.println(value);
i++;
}
其他回答
您可以将Guava库添加到项目中并使用列表。划分方法,例如:
List<Integer> bigList = ...
List<List<Integer>> smallerLists = Lists.partition(bigList, 10);
Apache Commons Collections 4在ListUtils类中有一个分区方法。下面是它的工作原理:
import org.apache.commons.collections4.ListUtils;
...
int targetSize = 100;
List<Integer> largeList = ...
List<List<Integer>> output = ListUtils.partition(largeList, targetSize);
private ArrayList<List<String>> chunkArrayList(ArrayList<String> arrayToChunk, int chunkSize) {
ArrayList<List<String>> chunkList = new ArrayList<>();
int guide = arrayToChunk.size();
int index = 0;
int tale = chunkSize;
while (tale < arrayToChunk.size()){
chunkList.add(arrayToChunk.subList(index, tale));
guide = guide - chunkSize;
index = index + chunkSize;
tale = tale + chunkSize;
}
if (guide >0) {
chunkList.add(arrayToChunk.subList(index, index + guide));
}
Log.i("Chunked Array: " , chunkList.toString());
return chunkList;
}
例子
ArrayList<String> test = new ArrayList<>();
for (int i=1; i<=1000; i++){
test.add(String.valueOf(i));
}
chunkArrayList(test,10);
输出
分块:[[1,2,3,4,5,6,7,8,9,10),(11、12、13、14、15、16、17、18、19、20],[21日,22日,23日,24日,25日,26日,27日,28日,29日,30日],[第三十一条、第三十二条、第三十三,34岁,35岁,36岁,37岁,38岁,39岁,40],[41、42、43、44、45、46岁,47岁,48岁,49岁,50],[51岁,52岁,53岁,54岁,55岁,56岁,57岁的58岁的59岁60],[61,62,63,64,65,66,67,68,69,70],[71,72,73,74,75,76,77,78,79,80],[81,82,83,84,85,86,87,88,89,90],[91,92,93,94,95,96,97,98,99,100 ], .........
你会在日志里看到的
Java 8
我们可以根据大小或条件拆分列表。
static Collection<List<Integer>> partitionIntegerListBasedOnSize(List<Integer> inputList, int size) {
return inputList.stream()
.collect(Collectors.groupingBy(s -> (s-1)/size))
.values();
}
static <T> Collection<List<T>> partitionBasedOnSize(List<T> inputList, int size) {
final AtomicInteger counter = new AtomicInteger(0);
return inputList.stream()
.collect(Collectors.groupingBy(s -> counter.getAndIncrement()/size))
.values();
}
static <T> Collection<List<T>> partitionBasedOnCondition(List<T> inputList, Predicate<T> condition) {
return inputList.stream().collect(Collectors.partitioningBy(s-> (condition.test(s)))).values();
}
然后我们可以把它们用作:
final List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
System.out.println(partitionIntegerListBasedOnSize(list, 4)); // [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10]]
System.out.println(partitionBasedOnSize(list, 4)); // [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10]]
System.out.println(partitionBasedOnSize(list, 3)); // [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
System.out.println(partitionBasedOnCondition(list, i -> i<6)); // [[6, 7, 8, 9, 10], [1, 2, 3, 4, 5]]
Java8流,一个表达式,没有其他库:
List<String> input = ...
int partitionSize = ...
Collection<List<String>> partitionedList = IntStream.range(0, input.size())
.boxed()
.collect(Collectors.groupingBy(partition -> (partition / partitionSize), Collectors.mapping(elementIndex -> input.get(elementIndex), Collectors.toList())))
.values();
测试:
List<String> input = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h" ,"i");
partitionSize = 1 - > [[a], [b], [c], [d], [e], [f], [g], [h],[我]] partitionSize = 2 - > [[a, b], c, d, e, f, g, h,[我]] partitionSize = 3 - > [a, b, c, d, e, f, g, h,我]] partitionSize = 7 - > [[a, b, c, d, e, f, g], [h,我]] partitionSize = 100 -> [[a, b, c, d, e, f, g, h, i]]