Efficient way to divide a list into lists of n size

前端 未结 14 1285
无人共我
无人共我 2020-11-27 16:59

I have an ArrayList, which I want to divide into smaller Lists of n size, and perform an operation on each. My current method of doing this is

implemented with Array

14条回答
  •  庸人自扰
    2020-11-27 17:15

    If you are working with a list I use the "Apache Commons Collections 4" library. It has a partition method in the ListUtils class:

    ...
    int targetSize = 100;
    List largeList = ...
    List> output = ListUtils.partition(largeList, targetSize);
    

    This method is adapted from http://code.google.com/p/guava-libraries/

提交回复
热议问题