Do the common terminal operations not work with Stream<java.lang.Byte>?

87 Views Asked by At

I have a simple Stream that is meant to work on Bytes:

List<Byte> byteList = Arrays.stream(new Byte[]{0x1, 0x2, 0x3, 0x4})
        .map(b -> b >> 1)
        .collect(Collectors.toList());

The compiler gives:

Error: incompatible types: inference variable T has incompatible bounds

equality constraints: java.lang.Byte

lower bounds: java.lang.Integer

And this also does not work:

Optional<Byte> aByte = Arrays.stream(new Byte[]{0x1, 0x2, 0x3, 0x4})
        .map(b -> b >> 1)
        .findFirst();

Error: incompatible types: java.util.Optional<java.lang.Integer> cannot be converted to java.util.Optional<java.lang.Byte>

I did not find any documentation stating that streams do not support Byte. Any pointers?

1

There are 1 best solutions below

3
On BEST ANSWER

b >> 1 returns an int which can't be automatically cast to byte. You can just add a cast:

.map(b -> (byte) (b >> 1))