我想我可以从this question推断,但我不能
我当然可以
short[] shortarray = {0,1,2};
List<Short> shortList = new ArrayList<Short>();
for (Short s : shortarray) {
shortList.add(s);
}但我想知道如何处理溪流。
List<Short> shortList = Arrays.stream(shortarray).boxed()
.collect(Collectors.toList());例如,它不能工作,但会产生The method stream(T[]) in the type Arrays is not applicable for the arguments (short[])
发布于 2020-02-05 09:21:30
好呀
IntStream.range(0, shortarray.length)
.mapToObj(s -> shortarray[s])
.collect(Collectors.toList());发布于 2020-02-05 09:21:59
至于为什么它不能与原始short[]一起工作:
没有流类型的短。流仅适用于非原语类型或IntStream、LongStream和DoubleStream。
要使其工作,您必须将短裤转换为具有兼容的流的数据类型(例如,简称),或者可能需要int来获取IntStream (请参阅ernest_k的答案)。
发布于 2020-02-05 09:38:38
还有一种方法可以将短数组转换为集合。
见下面的示例代码,
Short[] shortarray = {0,1,2};
List<Short> shortList1 = Arrays.asList(shortarray);
shortList1.stream().forEach(aShort -> {System.out.println(aShort);});`希望这能有所帮助。
谢谢桑迪
https://stackoverflow.com/questions/60072435
复制相似问题