【发布时间】:2018-07-21 04:04:59
【问题描述】:
假设我有字符串"5 12 4"。我想把它变成一个 ArrayList 的整数,在单个功能行中包含 5,12 和 4。
我觉得应该有办法做到这一点,将split(" ") 组合成stream,使用mapToInt(s->Integers.parseInt(s)) 和collect(Collectors.toList())。类似的东西:
ArrayList<Integer> nextLine = Arrays.stream(inputLine.split(" "))
.mapToInt(s->Integer.parseInt(s))
.collect(Collectors.toList());
但这不起作用,因为mapToInt 给我ints 而不是Integers。
我知道如何使用循环来做到这一点。如果存在,我想要一种在单个流操作中执行此操作的方法。
【问题讨论】:
标签: java string arraylist int java-stream