gpt4 book ai didi

java - 常见的终端操作是否不适用于 Stream

转载 作者:行者123 更新时间:2023-11-30 06:14:08 25 4
gpt4 key购买 nike

我有一个简单的 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 返回一个不能自动转换为 byte 的 int。您可以只添加类型转换:

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

关于java - 常见的终端操作是否不适用于 Stream<java.lang.Byte>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30799733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com