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

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
  • 374,785
  • 51
  • 663
  • 734
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
  • 5,890
  • 1
  • 28
  • 50
GotoFinal
  • 3,450
  • 2
  • 16
  • 31