How to split array list into equal parts?

2020-01-26 08: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.

9条回答
Root(大扎)
2楼-- · 2020-01-26 08:57

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

Thanks to @StuartMarks for the forgotten toList.

查看更多
啃猪蹄的小仙女
3楼-- · 2020-01-26 08:58

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)

查看更多
We Are One
4楼-- · 2020-01-26 08:58

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

查看更多
劫难
5楼-- · 2020-01-26 09:00

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

查看更多
爷、活的狠高调
6楼-- · 2020-01-26 09:04
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.

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2020-01-26 09:05

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
}
查看更多
登录 后发表回答