常见的终端操作对 Stream<java.lang.Byte> 不起作用吗?

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

我有一个简单的 Stream 用于处理字节:

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

编译器给出:

Error: incompatible types: inference variable T has incompatible bounds

equality constraints: java.lang.Byte

lower bounds: java.lang.Integer

这也行不通:

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>

我没有找到任何说明流不支持 Byte 的文档。有什么指点吗?

b >> 1 returns 无法自动转换为 byte 的 int。您可以只添加演员表:

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