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

ecd*_*dhe 2 java boxing java-8 java-stream

我试图将一个字节数组转换为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;
}
Run Code Online (Sandbox Code Playgroud)

第一行引发一条错误消息:

boxed()类型的方法未定义Stream<Byte>.

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

And*_*lko 5

该方法boxed()仅针对某些基本类型的流设计(IntStream,DoubleStream,和LongStream),以流的每个原始值框成相应的包装类(Integer,Double,和Long分别地).

该表达式Arrays.stream(toObjects(data))返回Stream<Byte>这已经是盒装的,因为那里是一个没有这样的事情ByteStream.