How to convert short[] into List<Short> in Java with streams?

peer :

I figured I could extrapolate from this question but I can't

I can of course do

short[] shortarray = {0,1,2};
List<Short> shortList = new ArrayList<Short>();
for (Short s : shortarray) {
    shortList.add(s);
}

But I'm wondering how to do it with streams.

List<Short> shortList = Arrays.stream(shortarray).boxed()
                              .collect(Collectors.toList());

doesn't work for example but yields The method stream(T[]) in the type Arrays is not applicable for the arguments (short[])

ernest_k :

Why not

IntStream.range(0, shortarray.length)
         .mapToObj(s -> shortarray[s])
         .collect(Collectors.toList());

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=5986&siteId=1