java - 转换 ArrayList> 到数组列表>
问题描述
我一直在尝试转换字符串ArrayList<ArrayList<String>> to ArrayList<ArrayList<Integer>>
这是我尝试构建的代码。
public void convertString (ArrayList<ArrayList<String>> templist) {
readList = new ArrayList<ArrayList<Integer>> ();
for (ArrayList<String> t : templist) {
readList.add(Integer.parseInt(t));
}
return readList;
需要一些关于如何转换它的建议。非常感谢。
解决方案
您可以使用 Stream API 实现此目的:
ArrayList<ArrayList<String>> list = ...
List<List<Integer>> result = list.stream()
.map(l -> l.stream().map(Integer::parseInt).collect(Collectors.toList()))
.collect(Collectors.toList());
或者如果你真的需要ArrayList
而不是List
:
ArrayList<ArrayList<String>> list = ...
ArrayList<ArrayList<Integer>> result = list.stream()
.map(l -> l.stream().map(Integer::parseInt).collect(Collectors.toCollection(ArrayList::new)))
.collect(Collectors.toCollection(ArrayList::new));
推荐阅读
- c# - 为什么要使用覆盖?
- sql-server - SQL Server 内存中 OLTP (Hekaton) 中的动态更新或等效脚本
- node.js - Angular 6+,能够从应用程序安全地创建用户
- ansible - 来自打包脚本的 Ansible 变量
- php - PHP 从 JSON 数组中获取值
- kubernetes - Rancher v2.0 提议 openebs 部署使用 v0.6.0 helm 模板
- javascript - 我的程序无法正常运行,当我单击“添加播放器”按钮时,没有文档添加到 mongo
- intellij-idea - intelliJ 将 Bootstrap 类显示为拼写错误
- java - 编辑jTable后如何将数据存入数组
- reactjs - 如果从本地提供图像,则加载速度比 CDN 快