java - 如何迭代一个大列表以使其更小以使用 Java 流进行 REST 调用?
问题描述
我有类似这样的逻辑来分块处理。
List<String> bigList = getList(); // Returns a big list of 1000 recs
int startIndex=0;
boolean entireListNotProcessed=false;
while(entireListNotProcessed) {
int endIndex = startIndex + 100;
if(endIndex > myList.size()-1) {
endIndex=myList.size()-1;
}
List<String> subList= bigList.subList(startIndex, endIndex);
//call Rest API with subList and update entireListNotProcessed flag...
}
有没有更好的方法来使用 java 流进行这个迭代?
解决方案
您可以使用apache commons中的 ListUtils 为您进行拆分(如果您真的需要,因为 1000 并不多)
List<List<Integer>> output = ListUtils.partition(largeList, targetSize);
推荐阅读
- excel - 按日期提取数据(今天函数)但不希望数据随时间变化
- javascript - CSS 通配符选择
- c# - 是否可以在 mvc 中有时间触发的操作方法?
- mysql - 无法在 Ubuntu 18.04 中重置 MySQL 根密码
- machine-learning - 用python完成第二个单词
- python - azure web 作业上的 python 脚本 - 没有名为 request 的模块
- python - Django POST 响应中的会话 ID 更改
- php - 从 S3 存储桶中获取 pdf 并以引导模式显示
- python - 如何处理动态变化的 xpath id?
- azure - Azure AD B2C 和本地 Active Directory