如何将数组列表拆分成相等的部分?

时间:2022-09-03 21:44:29

Is there anyway to split ArrayList into different parts without knowing size of it until runtime? I know there is a method called:

无论如何将ArrayList拆分成不同的部分而不知道它的大小直到运行时?我知道有一种叫做的方法:

list.subList(a,b);

but we need to explicitly mention staring and ending range of list. My problem is, we get a arraylist containing account numbers which is having data like 2000,4000 account numbers (there numbers will not be known during coding time), and I need to pass this acc nos into IN query of PL/SQL, as IN doesn't support more than 1000 values in it, I am trying to split into multiple chunks and sending it to query

但我们需要明确提到盯着和结束范围的清单。我的问题是,我们得到一个包含帐号的arraylist,其中包含2000,4000个帐号的数据(编码时间内不会知道数字),我需要将此符号传递给PL / SQL的IN查询,如IN不支持超过1000个值,我试图分成多个块并将其发送到查询

Note: I cannot use any external libraries like Guava etc.. :( Any guide in this regard is appreciated.

注意:我不能使用像番石榴等任何外部库.. :(在这方面的任何指南表示赞赏。

8 个解决方案

#1


57  

This should give you all your parts :

这应该给你所有的部分:

int partitionSize = 1000;
List<List<Integer>> partitions = new LinkedList<List<Integer>>();
for (int i = 0; i < originalList.size(); i += partitionSize) {
    partitions.add(originalList.subList(i,
            Math.min(i + partitionSize, originalList.size())));
}

#2


12  

generic function :

通用功能:

public static <T> ArrayList<T[]> chunks(ArrayList<T> bigList,int n){
    ArrayList<T[]> chunks = new ArrayList<T[]>();

    for (int i = 0; i < bigList.size(); i += n) {
        T[] chunk = (T[])bigList.subList(i, Math.min(bigList.size(), i + n)).toArray();         
        chunks.add(chunk);
    }

    return chunks;
}

enjoy it~ :)

享受吧〜:)

#3


5  

Java 8 (not that it has advantages):

Java 8(不是它有优势):

    List<String> list = new ArrayList<>();
    Collections.addAll(list,  "a","b","c","b","c","a","c","a","b");

Grouping size:

分组大小:

    final int G = 3;
    final int NG = (list.size() + G - 1) / G;

In old style:

旧式:

    List<List<String>> result = new ArrayList(NG);
    IntStream.range(0, list.size())
        .forEach(i -> {
            if (i % G == 0) {
                result.add(i/G, new ArrayList<>());
            }
            result.get(i/G).add(list.get(i));
        });

In new style:

新风格:

    List<List<String>> result = IntStream.range(0, NG)
        .mapToObj(i -> list.subList(3 * i, Math.min(3 * i + 3, list.size())))
        .collect(Collectors.toList());

Thanks to @StuartMarks for the forgotten toList.

感谢@StuartMarks为忘记了toList。

#4


4  

If you're constrained by PL/SQL in limits then you want to know how to split a list into chunks of size <=n, where n is the limit. This is a much simpler problem as it does not require knowing the size of the list in advance.

如果您受限于PL / SQL限制,那么您想知道如何将列表拆分为大小<= n的块,其中n是限制。这是一个更简单的问题,因为它不需要事先知道列表的大小。

Pseudocode:

伪代码:

for (int n=0; n<list.size(); n+=limit)
{
    chunkSize = min(list.size,n+limit);
    chunk     = list.sublist(n,chunkSize);
    // do something with chunk
}

#5


2  

If you already have or don't mind adding the Guava library, you don't need to reinvent the wheel.

如果您已经或不介意添加Guava库,则无需重新发明*。

Simply do: final List<List<String>> splittedList = Lists.partition(bigList, 10);

简单地说:final List > splittedList = Lists.partition(bigList,10);

where bigList implements the List interface and 10 is the desired size of each sublist (the last may be smaller)

其中bigList实现List接口,10是每个子列表的所需大小(最后一个可能更小)

#6


0  

listSize = oldlist.size();
chunksize =1000;
chunks = list.size()/chunksize;
ArrayList subLists;
ArrayList finalList;
int count = -1;
for(int i=0;i<chunks;i++){
     subLists = new ArrayList();
     int j=0;
     while(j<chunksize && count<listSize){
        subList.add(oldList.get(++count))
        j++;
     }
     finalList.add(subLists)
}

You can use this finalList as it contains the list of chuncks of the oldList.

您可以使用此finalList,因为它包含oldList的chuncks列表。

#7


0  

I am also doing key:value mapping for values with index.

我也在做关键:索引值的值映射。

  public static void partitionOfList(List<Object> l1, List<Object> l2, int partitionSize){
            Map<String, List<Object>> mapListData = new LinkedHashMap<String, List<Object>>();
            List<Object> partitions = new LinkedList<Object>();
            for (int i = 0; i < l1.size(); i += partitionSize) {
                partitions.add(l1.subList(i,Math.min(i + partitionSize, l1.size())));
                l2=new ArrayList(partitions);
            }
            int l2size = l2.size();
            System.out.println("Partitioned List: "+l2);
            int j=1;
            for(int k=0;k<l2size;k++){
                 l2=(List<Object>) partitions.get(k);
                // System.out.println(l2.size());
                 if(l2.size()>=partitionSize && l2.size()!=1){
                mapListData.put("val"+j+"-val"+(j+partitionSize-1), l2);
                j=j+partitionSize;
                 }
                 else if(l2.size()<=partitionSize && l2.size()!=1){
                    // System.out.println("::::@@::"+ l2.size());
                     int s = l2.size();
                     mapListData.put("val"+j+"-val"+(j+s-1), l2);
                        //k++;
                        j=j+partitionSize;
                 }
                 else if(l2.size()==1){
                    // System.out.println("::::::"+ l2.size());
                     //int s = l2.size();
                     mapListData.put("val"+j, l2);
                        //k++;
                        j=j+partitionSize;
                 }
            }
            System.out.println("Map: " +mapListData);
        }

    public static void main(String[] args) {
            List l1 = new LinkedList();
            l1.add(1);
            l1.add(2);
            l1.add(7);
            l1.add(4);
            l1.add(0);
            l1.add(77);
            l1.add(34);

    partitionOfList(l1,l2,2);
    }

Output:

输出:

Partitioned List: [[1, 2], [7, 4], [0, 77], [34]]

分区列表:[[1,2],[7,4],[0,77],[34]]

Map: {val1-val2=[1, 2], val3-val4=[7, 4], val5-val6=[0, 77], val7=[34]}

地图:{val1-val2 = [1,2],val3-val4 = [7,4],val5-val6 = [0,77],val7 = [34]}

#8


-1  

generic method for your help :

通用方法为您提供帮助:

private static List<List<Object>> createBatch(List<Object> originalList,
        int chunkSize) {
    List<List<Object>> listOfChunks = new ArrayList<List<Object>>();
    for (int i = 0; i < originalList.size() / chunkSize; i++) {
        listOfChunks.add(originalList.subList(i * chunkSize, i * chunkSize
                + chunkSize));
    }
    if (originalList.size() % chunkSize != 0) {
        listOfChunks.add(originalList.subList(originalList.size()
                - originalList.size() % chunkSize, originalList.size()));
    }
    return listOfChunks;

#1


57  

This should give you all your parts :

这应该给你所有的部分:

int partitionSize = 1000;
List<List<Integer>> partitions = new LinkedList<List<Integer>>();
for (int i = 0; i < originalList.size(); i += partitionSize) {
    partitions.add(originalList.subList(i,
            Math.min(i + partitionSize, originalList.size())));
}

#2


12  

generic function :

通用功能:

public static <T> ArrayList<T[]> chunks(ArrayList<T> bigList,int n){
    ArrayList<T[]> chunks = new ArrayList<T[]>();

    for (int i = 0; i < bigList.size(); i += n) {
        T[] chunk = (T[])bigList.subList(i, Math.min(bigList.size(), i + n)).toArray();         
        chunks.add(chunk);
    }

    return chunks;
}

enjoy it~ :)

享受吧〜:)

#3


5  

Java 8 (not that it has advantages):

Java 8(不是它有优势):

    List<String> list = new ArrayList<>();
    Collections.addAll(list,  "a","b","c","b","c","a","c","a","b");

Grouping size:

分组大小:

    final int G = 3;
    final int NG = (list.size() + G - 1) / G;

In old style:

旧式:

    List<List<String>> result = new ArrayList(NG);
    IntStream.range(0, list.size())
        .forEach(i -> {
            if (i % G == 0) {
                result.add(i/G, new ArrayList<>());
            }
            result.get(i/G).add(list.get(i));
        });

In new style:

新风格:

    List<List<String>> result = IntStream.range(0, NG)
        .mapToObj(i -> list.subList(3 * i, Math.min(3 * i + 3, list.size())))
        .collect(Collectors.toList());

Thanks to @StuartMarks for the forgotten toList.

感谢@StuartMarks为忘记了toList。

#4


4  

If you're constrained by PL/SQL in limits then you want to know how to split a list into chunks of size <=n, where n is the limit. This is a much simpler problem as it does not require knowing the size of the list in advance.

如果您受限于PL / SQL限制,那么您想知道如何将列表拆分为大小<= n的块,其中n是限制。这是一个更简单的问题,因为它不需要事先知道列表的大小。

Pseudocode:

伪代码:

for (int n=0; n<list.size(); n+=limit)
{
    chunkSize = min(list.size,n+limit);
    chunk     = list.sublist(n,chunkSize);
    // do something with chunk
}

#5


2  

If you already have or don't mind adding the Guava library, you don't need to reinvent the wheel.

如果您已经或不介意添加Guava库,则无需重新发明*。

Simply do: final List<List<String>> splittedList = Lists.partition(bigList, 10);

简单地说:final List > splittedList = Lists.partition(bigList,10);

where bigList implements the List interface and 10 is the desired size of each sublist (the last may be smaller)

其中bigList实现List接口,10是每个子列表的所需大小(最后一个可能更小)

#6


0  

listSize = oldlist.size();
chunksize =1000;
chunks = list.size()/chunksize;
ArrayList subLists;
ArrayList finalList;
int count = -1;
for(int i=0;i<chunks;i++){
     subLists = new ArrayList();
     int j=0;
     while(j<chunksize && count<listSize){
        subList.add(oldList.get(++count))
        j++;
     }
     finalList.add(subLists)
}

You can use this finalList as it contains the list of chuncks of the oldList.

您可以使用此finalList,因为它包含oldList的chuncks列表。

#7


0  

I am also doing key:value mapping for values with index.

我也在做关键:索引值的值映射。

  public static void partitionOfList(List<Object> l1, List<Object> l2, int partitionSize){
            Map<String, List<Object>> mapListData = new LinkedHashMap<String, List<Object>>();
            List<Object> partitions = new LinkedList<Object>();
            for (int i = 0; i < l1.size(); i += partitionSize) {
                partitions.add(l1.subList(i,Math.min(i + partitionSize, l1.size())));
                l2=new ArrayList(partitions);
            }
            int l2size = l2.size();
            System.out.println("Partitioned List: "+l2);
            int j=1;
            for(int k=0;k<l2size;k++){
                 l2=(List<Object>) partitions.get(k);
                // System.out.println(l2.size());
                 if(l2.size()>=partitionSize && l2.size()!=1){
                mapListData.put("val"+j+"-val"+(j+partitionSize-1), l2);
                j=j+partitionSize;
                 }
                 else if(l2.size()<=partitionSize && l2.size()!=1){
                    // System.out.println("::::@@::"+ l2.size());
                     int s = l2.size();
                     mapListData.put("val"+j+"-val"+(j+s-1), l2);
                        //k++;
                        j=j+partitionSize;
                 }
                 else if(l2.size()==1){
                    // System.out.println("::::::"+ l2.size());
                     //int s = l2.size();
                     mapListData.put("val"+j, l2);
                        //k++;
                        j=j+partitionSize;
                 }
            }
            System.out.println("Map: " +mapListData);
        }

    public static void main(String[] args) {
            List l1 = new LinkedList();
            l1.add(1);
            l1.add(2);
            l1.add(7);
            l1.add(4);
            l1.add(0);
            l1.add(77);
            l1.add(34);

    partitionOfList(l1,l2,2);
    }

Output:

输出:

Partitioned List: [[1, 2], [7, 4], [0, 77], [34]]

分区列表:[[1,2],[7,4],[0,77],[34]]

Map: {val1-val2=[1, 2], val3-val4=[7, 4], val5-val6=[0, 77], val7=[34]}

地图:{val1-val2 = [1,2],val3-val4 = [7,4],val5-val6 = [0,77],val7 = [34]}

#8


-1  

generic method for your help :

通用方法为您提供帮助:

private static List<List<Object>> createBatch(List<Object> originalList,
        int chunkSize) {
    List<List<Object>> listOfChunks = new ArrayList<List<Object>>();
    for (int i = 0; i < originalList.size() / chunkSize; i++) {
        listOfChunks.add(originalList.subList(i * chunkSize, i * chunkSize
                + chunkSize));
    }
    if (originalList.size() % chunkSize != 0) {
        listOfChunks.add(originalList.subList(originalList.size()
                - originalList.size() % chunkSize, originalList.size()));
    }
    return listOfChunks;