将一个长的list转化为定长的list,分批处理

业务背景

需要向某个接口传输一批数据或者批量插入一批数据,但一次能处理或者接收的数据是有限的,有最大长度。所以,需要我们将这批长长的数据,转化为若干段定长的数据段,一批一批的处理

代码实现(java)

package listTest;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Since: 2021/12/29
 * @Author:
 */
@Slf4j
public class SubListTransfer {
    private static final Integer LIST_SIZE = 30;
    private static final Integer STANDER_LENGTH = 14;

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>(LIST_SIZE);
        for (int i = 0; i < LIST_SIZE; i++) {
            list.add(i);
        }
        if (CollectionUtils.isEmpty(list)) {
            log.info("集合 为空,不进行任何操作");
            return;
        }
        int size = list.size();
        log.info("Operation start---");
        for (int i = 0; ; i++) {
            int head = i * STANDER_LENGTH;
            int tail = (i + 1) * STANDER_LENGTH;
            log.info("{} 次传输", i + 1);
            if ((size > head) && (size <= tail)) {
                List<Integer> subList = list.subList(head, size);
                // doSomething 代表业务操作
                doSomething(subList);
                break;
            } else {
                List<Integer> subList = list.subList(head, tail);
                doSomething(subList);
            }
        }

        log.info("Operation end---");

    }

    private static void doSomething(List<Integer> subList) {
        subList.stream().forEach(value -> {
            log.info("打印list的值: {}", value);
        });
        log.info("doSomething end ... ");
    }
}

运行效果

19:30:31.029 [main] INFO listTest.SubListTransfer - Operation start---
19:30:31.040 [main] INFO listTest.SubListTransfer - 1 次传输
19:30:31.468 [main] INFO listTest.SubListTransfer - 打印list的值: 0
19:30:31.469 [main] INFO listTest.SubListTransfer - 打印list的值: 1
19:30:31.469 [main] INFO listTest.SubListTransfer - 打印list的值: 2
19:30:31.469 [main] INFO listTest.SubListTransfer - 打印list的值: 3
19:30:31.469 [main] INFO listTest.SubListTransfer - 打印list的值: 4
19:30:31.469 [main] INFO listTest.SubListTransfer - 打印list的值: 5
19:30:31.469 [main] INFO listTest.SubListTransfer - 打印list的值: 6
19:30:31.469 [main] INFO listTest.SubListTransfer - 打印list的值: 7
19:30:31.469 [main] INFO listTest.SubListTransfer - 打印list的值: 8
19:30:31.469 [main] INFO listTest.SubListTransfer - 打印list的值: 9
19:30:31.469 [main] INFO listTest.SubListTransfer - 打印list的值: 10
19:30:31.469 [main] INFO listTest.SubListTransfer - 打印list的值: 11
19:30:31.469 [main] INFO listTest.SubListTransfer - 打印list的值: 12
19:30:31.469 [main] INFO listTest.SubListTransfer - 打印list的值: 13
19:30:31.469 [main] INFO listTest.SubListTransfer - doSomething end ... 
19:30:31.469 [main] INFO listTest.SubListTransfer - 2 次传输
19:30:31.469 [main] INFO listTest.SubListTransfer - 打印list的值: 14
19:30:31.470 [main] INFO listTest.SubListTransfer - 打印list的值: 15
19:30:31.470 [main] INFO listTest.SubListTransfer - 打印list的值: 16
19:30:31.470 [main] INFO listTest.SubListTransfer - 打印list的值: 17
19:30:31.470 [main] INFO listTest.SubListTransfer - 打印list的值: 18
19:30:31.470 [main] INFO listTest.SubListTransfer - 打印list的值: 19
19:30:31.470 [main] INFO listTest.SubListTransfer - 打印list的值: 20
19:30:31.470 [main] INFO listTest.SubListTransfer - 打印list的值: 21
19:30:31.470 [main] INFO listTest.SubListTransfer - 打印list的值: 22
19:30:31.470 [main] INFO listTest.SubListTransfer - 打印list的值: 23
19:30:31.470 [main] INFO listTest.SubListTransfer - 打印list的值: 24
19:30:31.470 [main] INFO listTest.SubListTransfer - 打印list的值: 25
19:30:31.470 [main] INFO listTest.SubListTransfer - 打印list的值: 26
19:30:31.470 [main] INFO listTest.SubListTransfer - 打印list的值: 27
19:30:31.470 [main] INFO listTest.SubListTransfer - doSomething end ... 
19:30:31.470 [main] INFO listTest.SubListTransfer - 3 次传输
19:30:31.470 [main] INFO listTest.SubListTransfer - 打印list的值: 28
19:30:31.470 [main] INFO listTest.SubListTransfer - 打印list的值: 29
19:30:31.470 [main] INFO listTest.SubListTransfer - doSomething end ... 
19:30:31.470 [main] INFO listTest.SubListTransfer - Operation end---

结语

欢迎大家留言讨论,或批评指正,期待共同进步!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值