0
  1. Why Stream.of(arr) returns a Stream<short[]> instead of Stream<Short> ?

    void f(short... arr){
        Stream<short[]> s = Stream.of(arr);
    }
    
  2. How to convert short[] to Stream<Short>?

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
J. Doe
  • 125
  • 1
  • 9

2 Answers2

3

When using Stream.of(T... values), T must be a reference type not a primitive, type. short is a primitive type, whereas short[] is a reference type, so Stream.of(shortArray) gives you a Stream<short[]> as you have noticed.

One way to convert a short[] to a Stream<Short> is

Stream<Short> stream = IntStream.range(0, shortArray.length).mapToObj(i -> shortArray[i]);

However, I would question why you are using short at all.

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
2

Note that Short doesn't make part of number wrapper classes that have their own Stream class (such as IntStream or LongStream).
So you should write more code.

You could for example use mapToObj() to box short to Short :

short[] shorts = ...;
Stream<Short> shortStream = IntStream.range(0, shorts.length)
                                     .mapToObj(i -> shorts[i]);
davidxxx
  • 125,838
  • 23
  • 214
  • 215