java - 如何使用流 API 将数组划分为子数组
问题描述
我有一个大小为 1000 的数组。我想使用流操作来执行如下操作:-
List list= new ArrayList();
//list is initialize to 1000 elements
List subList = list.subList(0, 100);
// perform some operaions over the subarray
List subList1 = list.subList(101, 200);
// perform some operaions over the subarray
.... so on
}
我想要使用流 API 的代码。提前致谢
解决方案
关于什么 :
List<List<Integer>> result = IntStream.range(0, list.size() / 100)
.mapToObj(index -> list.subList(index * 100, index * 100 + 100))
.collect(Collectors.toList());
推荐阅读
- java - Android Studio 中的错误和模拟器问题
- firebase - 在颤动的文本字段中显示值
- java - 将gridview与其他项目一起使用时滚动整个屏幕
- python - 来自 HTML 颜色的 seaborn 发散 cmap 调色板
- javascript - 如何对数组中的相同元素进行分组
- selenium - 使用 Selenium Web 驱动程序和 Firefox 时出现空白页
- javascript - 在 HTML 视图中的特定文本之前添加行距
- api - 拥有一个 facebook 业务管理帐户来使用 WhatsApp Business API 是否有必要?
- sql - 如何从带有条件的sql表中删除重复项
- delphi - 如何启用多个位按钮?