32

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

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

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

4

9 回答 9

77

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())));
}
于 2012-12-03T06:59:34.680 回答
13

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~ :)

于 2014-01-20T12:09:09.020 回答
8

Java 8 (not that it has advantages):

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(G * i, Math.min(G * i + G, list.size())))
    .collect(Collectors.toList());

Thanks to @StuartMarks for the forgotten toList.

于 2016-08-30T07:08:03.653 回答
6

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.

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
}
于 2012-12-03T06:46:39.757 回答
5

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

Simply do: final List<List<String>> 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)

于 2016-12-23T17:05:29.920 回答
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.

于 2012-12-03T07:05:57.590 回答
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]]

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

于 2016-03-04T13:12:05.023 回答
0

The following code:

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

produces the following output:

  //[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]  
于 2019-02-21T07:59:21.720 回答
-2

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;
于 2013-07-31T12:00:25.453 回答