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

时间:2012-12-03 06:43:29

标签: java plsql oracle10g

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

list.subList(a,b);

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

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

9 个答案:

答案 0 :(得分:64)

这应该为您提供所有部分:

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())));
}

答案 1 :(得分:13)

通用功能:

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;
}

享受它〜:)

答案 2 :(得分:7)

Java 8(不是它有优势):

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

分组大小:

    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));
        });

旧式:

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

新风格:

tbody

感谢@StuartMarks忘记了toList。

答案 3 :(得分:4)

如果您受到PL / SQL in限制的限制,那么您想知道如何将列表拆分为大小为&lt; = n 的块,其中 n < / em>是限制。这是一个更简单的问题,因为它需要提前知道列表的大小。

伪代码:

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

答案 4 :(得分:4)

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

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

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

答案 5 :(得分: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)
}

您可以使用此决赛入围者,因为它包含oldList的块列表。

答案 6 :(得分:0)

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

  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);
    }

输出:

  

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

     

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

答案 7 :(得分:0)

以下代码:

  private static List<List<Object>> createBatch(List<Object> originalList, int 
  batch_size) {
    int Length = originalList.size();
    int chunkSize = Length / batch_size;
    int residual = Length-chunkSize*batch_size;
    List<Integer> list_nums = new ArrayList<Integer>();
    for (int i = 0; i < batch_size; i++) {
        list_nums.add(chunkSize);
    }
    for (int i = 0; i < residual; i++) {
        list_nums.set(i, list_nums.get(i) + 1);
    }
    List<Integer> list_index = new ArrayList<Integer>();
    int cumulative = 0;
    for (int i = 0; i < batch_size; i++) {
        list_index.add(cumulative);
        cumulative += list_nums.get(i);
    }
    list_index.add(cumulative);
    List<List<Object>> listOfChunks = new ArrayList<List<Object>>();
    for (int i = 0; i < batch_size; i++) {
        listOfChunks.add(originalList.subList(list_index.get(i), 
 list_index.get(i + 1)));
    }
    return listOfChunks;
  }

产生以下输出:

  //[0,..,99] equally partition into 6 batch
  // result:batch_size=[17,17,17,17,16,16]
  //Continually partition into 6 batch, and residual also equally 
  //partition into top n batch
  // Output:
  [0,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,31,32,33] 
  [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]  

答案 8 :(得分:-2)

您的帮助的通用方法:

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;