如何将一个数组列表(size=1000)拆分为多个相同大小(=10)的数组列表?

ArrayList<Integer> results;

当前回答

Java8流,一个表达式,没有其他库(两个解决方案,无需创建不必要的映射):

List<List<Integer>> partitionedList = IntStream.range(0, (list.size()-1)/targetSize+1)
        .mapToObj(i -> list.subList(i*targetSize, Math.min(i*targetSize+targetSize, list.size())))
        .collect(Collectors.toList());

List<List<Integer>> partitionedList2 = IntStream.iterate(0, i -> i < list.size(), i -> i + targetSize)
        .mapToObj(i -> list.subList(i, Math.min(i + targetSize, list.size())))
        .collect(Collectors.toList());

请记住,这些是子列表,因此对原始列表的更改也会影响这些子列表。

如果你不希望它们是子列表,而是新创建的独立列表,可以这样修改:

List<List<Integer>> partitionedList = IntStream.range(0, (list.size()-1)/targetSize+1)
        .mapToObj(i -> IntStream.range(i*targetSize, Math.min(i*targetSize+targetSize, list.size())).mapToObj(j -> list.get(j)).collect(Collectors.toList()))
        .collect(Collectors.toList());

List<List<Integer>> partitionedList2 = IntStream.iterate(0, i -> i < list.size(), i -> i + targetSize)
        .mapToObj(i -> IntStream.range(i, Math.min(i + targetSize, list.size())).mapToObj(j -> list.get(j)).collect(Collectors.toList()))
        .collect(Collectors.toList());

其他回答

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]]

这里讨论了一个类似的问题,Java:将List拆分为两个子列表?

主要可以使用子列表。更多细节:subblist

返回该列表中frommindex(包含)和toIndex(不包含)之间部分的视图。(如果fromIndex和toIndex相等,返回的列表为空。)返回的列表受此列表支持,因此返回列表中的更改将反映在此列表中,反之亦然。返回的列表支持该列表支持的所有可选列表操作…

您可以将Guava库添加到项目中并使用列表。划分方法,例如:

List<Integer> bigList = ...
List<List<Integer>> smallerLists = Lists.partition(bigList, 10);
    **Divide a list to lists of n size**

    import java.util.AbstractList;
    import java.util.ArrayList;
    import java.util.List;

    public final class PartitionUtil<T> extends AbstractList<List<T>> {

        private final List<T> list;
        private final int chunkSize;

        private PartitionUtil(List<T> list, int chunkSize) {
            this.list = new ArrayList<>(list);
            this.chunkSize = chunkSize;
        }

        public static <T> PartitionUtil<T> ofSize(List<T> list, int chunkSize) {
            return new PartitionUtil<>(list, chunkSize);
        }

        @Override
        public List<T> get(int index) {
            int start = index * chunkSize;
            int end = Math.min(start + chunkSize, list.size());

            if (start > end) {
                throw new IndexOutOfBoundsException("Index " + index + " is out of the list range <0," + (size() - 1) + ">");
            }

            return new ArrayList<>(list.subList(start, end));
        }

        @Override
        public int size() {
            return (int) Math.ceil((double) list.size() / (double) chunkSize);
        }
    }





Function call : 
              List<List<String>> containerNumChunks = PartitionUtil.ofSize(list, 999)

详情:https://e.printstacktrace.blog/divide-a-list-to-lists-of-n-size-in-Java-8/

你可以使用Eclipse Collections中的chunk方法:

ArrayList<Integer> list = new ArrayList<>(Interval.oneTo(1000));
RichIterable<RichIterable<Integer>> chunks = Iterate.chunk(list, 10);
Verify.assertSize(100, chunks);

这篇DZone文章中还包含了一些块方法的示例。

注意:我是Eclipse Collections的提交者。