我正在尝试将字节数组转换为LinkedList<Byte>

//data is of type byte[]
List<Byte> list = Arrays.stream(toObjects(data)).boxed().collect(Collectors.toList());

Byte[] toObjects(byte[] bytesPrim) {
    Byte[] bytes = new Byte[bytesPrim.length];
    Arrays.setAll(bytes, n -> bytesPrim[n]);

    return bytes;
}


第一行引发一条错误消息,指出:


  对于类型boxed(),未定义方法Stream<Byte>


知道为什么我会收到此错误消息以及如何规避此问题吗?

最佳答案

方法boxed()仅设计用于某些基本类型(IntStreamDoubleStreamLongStream)的流,以将流的每个原始值放入相应的包装类(IntegerDouble,和Long分别)。

表达式Arrays.stream(toObjects(data))返回一个Stream<Byte>,该ByteStreamthere is no such thing as a class起就已装箱。

09-04 06:06