Efficient way to divide a list into lists of n size

前端 未结 14 1274
无人共我
无人共我 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:20

    Since you want to optimise your performance you should use a parallel stream instead of a for loop. This way you can use multiple threads.

    Lists.partition(A, n).parallelStream().forEach({
        //do stuff with temp
    });
    

    You can also use other ways to wort with the stream for example collect or map if it matches your purpose.

提交回复
热议问题