How to split array list into equal parts?

前端 未结 9 1318
攒了一身酷
攒了一身酷 2020-11-29 07:20

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


        
9条回答
  •  悲哀的现实
    2020-11-29 08:04

    This should give you all your parts :

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

提交回复
热议问题