我一直在尝试转换 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));
关于java - 将 ArrayList<ArrayList<String>> 转换为 ArrayList<ArrayList<Integer>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59566928/