业务背景
需要向某个接口传输一批数据或者批量插入一批数据,但一次能处理或者接收的数据是有限的,有最大长度。所以,需要我们将这批长长的数据,转化为若干段定长的数据段,一批一批的处理
代码实现(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---
结语
欢迎大家留言讨论,或批评指正,期待共同进步!