2

I have a variable of type byte[], not Byte[].

I'm trying to use Arrays::stream method to process this array with lambda.

However, there's no such reload of Arrays::stream that takes byte[] as parameter.

The reload Arrays::stream(T[] data) also does not work.
I guess it's because byte[] is an array of java prime type byte, which cannot be treated as generic type parameter T.

I tried to cast byte[] to Byte[] or int[], which all failed as well.

Amalius
  • 17
  • 6
Shiyao Wang
  • 29
  • 1
  • 7

2 Answers2

6

You can create an IntStream:

byte[] bytearr = new byte[10];
IntStream ints = IntStream.range (0, bytearr.length).map (i->bytearr[i]);

or a Stream<Byte>:

byte[] bytearr = new byte[10];
Stream<Byte> bytes = IntStream.range (0, bytearr.length).mapToObj (i->bytearr[i]);
Eran
  • 387,369
  • 54
  • 702
  • 768
1

You can't, there is no ByteStream in java, there is only Stream<T>, IntStream, DoubleStream and LongStream.

Just do this with normal loops, unless you want to implement that class manually.
Or you can convert that to Byte[] but this will be huge waste of time and memory. Same with converting to int[] and using IntStream, but smaller cost than Byte[].

But there is no reason to do something like that unless you are forced to do this that way, it might affect performance and memory footprint a lot for bigger arrays.

Hulk
  • 6,399
  • 1
  • 30
  • 52
GotoFinal
  • 3,585
  • 2
  • 18
  • 33