简体   繁体   English

Java8中子列表的分区列表

[英]Partition list to sub lists in Java8

I'm wondering if there is any nice a way using Java 8 to do something like this:我想知道是否有使用 Java 8 做这样的事情的好方法:

int amount = 1000;
int firstIndex = 0;
int lastIndex = firstIndex + amount;
List<int> projectIdsList;

while (lastIndex < projectIdsList.size()){
        doSomethingWhitSubList(projectIdsList.subList(firstIndex, lastIndex));
        firstIndex = lastIndex;
        lastIndex = firstIndex + amount;
}
doSomethingWhitSubList(projectIdsList.subList(firstIndex,  projectIdsList.size());

You can partition the list into sublist using IntStream , and I'm not really sure about your intention after partition, but you can collect the sublist's into a list using collect您可以使用IntStream将列表划分为子列表,我不太确定您在分区后的意图,但您可以使用collect将子列表收集到列表中

List<List<Integer>> subLists = IntStream.range(0,integers.size())
            .filter(i->i%amount == 0)
            .mapToObj(i->integers.subList(i,i+amount > integers.size() ? integers.size() : i+amount))
            .collect(Collectors.toList());

Or you can also use forEach for any operations after partitioning或者您也可以使用forEach进行分区后的任何操作

IntStream.range(0,integers.size())
            .filter(i->i%amount == 0)
            .mapToObj(i->integers.subList(i,i+amount > integers.size() ? integers.size() : i+amount))
            .forEach(this::doSomethingWhitSubList);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM