Java 在调用每个块上的方法之前,如何将列表切成块?

Java 在调用每个块上的方法之前,如何将列表切成块?,java,algorithm,list,Java,Algorithm,List,我在Java中有一个对象列表: ArrayList<myObj> objs = generateObjs(); 我想拆分列表objs,这样我就可以在一组五个元素中发送更多的对象 最好的方法是什么 我考虑过实施这样的东西: public void sendSliced(List objs) { ArrayList<myObj> tempList = new ArrayList()<>; for (int i = 0; i < objs.

我在
Java
中有一个
对象列表:

ArrayList<myObj> objs = generateObjs();
我想拆分列表
objs
,这样我就可以在一组五个元素中发送更多的对象

最好的方法是什么

我考虑过实施这样的东西:

public void sendSliced(List objs) {

    ArrayList<myObj> tempList = new ArrayList()<>;
    for (int i = 0; i < objs.size(); i++) {
        tempList.add(objs.get(i));
        if (i % 5 == 0) {
            sendObjectsFurther(tempList);
            tempList.clear();
        }         
    }
}
public void sendslicated(列表对象){
ArrayList tempList=新的ArrayList();
对于(int i=0;i
但我认为它不能覆盖所有的边缘情况,你能帮我吗?谢谢

您可以使用它

List<myObj> objs = generateObjs();
int chunkSize = 5;
AtomicInteger counter = new AtomicInteger();

Collection<List<myObj>> result = objs.stream()
    .collect(Collectors.groupingBy(it -> counter.getAndIncrement() / chunkSize))
    .values();
for (myObj chunk: result){
    sendObjectsFurther(tempList);
}
List objs=generateObjs();
int chunkSize=5;
AtomicInteger计数器=新的AtomicInteger();
收集结果=objs.stream()
.collect(Collectors.groupingBy(it->counter.getAndIncrement()/chunkSize))
.values();
for(myObj块:结果){
sendObjectsFurther(圣殿骑士);
}

此外,类名应以大写字母开头。

您可以使用guava Lists.partition,它将拆分为子列表。 例如:

for (List<String> slice : Lists.partition(bigList, 5)) {
    send(slice);
}
for(列表切片:Lists.partition(bigList,5)){
发送(切片);
}

相关的javadoc链接

请检查下面的代码,这里每个子列表将表示一个包含5个元素的区块,因此假设我的obj列表中总共有8个元素,那么第一个区块将有5个,下一个区块将有3个

public static void main(String[] args) throws IOException {
            List<Employee> objs = new ArrayList<>();
            objs.add(new Employee(1));
            objs.add(new Employee(2));
            objs.add(new Employee(3));
            objs.add(new Employee(4));
            objs.add(new Employee(5));
            objs.add(new Employee(6));
            objs.add(new Employee(7));
            objs.add(new Employee(8));
            sendSliced(objs);
        }

        private static void sendSliced(List<Employee> objs) {
            int chunkSize = 5;
            final AtomicInteger counter = new AtomicInteger();
            Collection<List<Employee>> subLists = objs.stream()
                    .collect(Collectors.groupingBy(it -> counter.getAndIncrement() / chunkSize))
                    .values();
            subLists.stream().forEach( sublist -> System.out.print(sublist));
            subLists.stream().forEach( sublist -> sendObjectsFurther(sublist));
        }
}


class Employee {
   private int id;
   Employee(int id) {
    this.id = id;
   }    
}

您可以在代码中添加一行

public void sendSliced(List objs) {

    ArrayList<myObj> tempList = new ArrayList()<>;
    for (int i = 0; i < objs.size(); i++) {
        tempList.add(objs.get(i));
        if (i % 5 == 0) {
            sendObjectsFurther(tempList);
            tempList.clear();
        }         
    }
    sendObjectsFurther(tempList);
}
public void sendslicated(列表对象){
ArrayList tempList=新的ArrayList();
对于(int i=0;i

这将添加剩余的项目

写一个测试来检查所有的“边缘案例”,看看他们是否通过了测试。如果没有,请尝试调试,如果无法找到错误源,请返回。提示如果最后一个块的大小为@randomuser1,那么您的TempList声明也是错误的,而不是“ArrayList TempList=new ArrayList();”它应该是“ArrayList TempList=new ArrayList();这看起来很好!但是有一个问题-当最后一个区块<5时,这会起作用吗?例如,如果您的列表有12个项目,那么最后一个区块的大小将为2。我只是好奇,为什么在使用普通流时在这里使用
AtomicInteger
?我认为这在这种情况下不是必需的,但可能有利于并发扩展。我修改了答案中的链接中提供了示例。
index = 0
[com.practice.stackoverflow.Employee@77459877, com.practice.stackoverflow.Employee@5b2133b1, com.practice.stackoverflow.Employee@72ea2f77, com.practice.stackoverflow.Employee@33c7353a, com.practice.stackoverflow.Employee@681a9515]
index=1
[com.practice.stackoverflow.Employee@3af49f1c, com.practice.stackoverflow.Employee@19469ea2, com.practice.stackoverflow.Employee@13221655]  
public void sendSliced(List objs) {

    ArrayList<myObj> tempList = new ArrayList()<>;
    for (int i = 0; i < objs.size(); i++) {
        tempList.add(objs.get(i));
        if (i % 5 == 0) {
            sendObjectsFurther(tempList);
            tempList.clear();
        }         
    }
    sendObjectsFurther(tempList);
}