没有方法来获取字节数组的流

Cod*_*roc 2 java java-8

我想得到字节数组流,但我知道Arrays没有方法来获取字节数组的流.

byte[] byteArr = new byte[100];
Arrays.stream(byteArr);//Compile time error
Run Code Online (Sandbox Code Playgroud)

我的问题,

  • 为什么不支持此功能?
  • 如何获取字节数组的流?

注意:我知道我可以使用Byte[]而不是byte[]但不能回答我的问题.

JB *_*zet 5

只有3种类型的基本流:IntStream,LongStream和DoubleStream.

因此,您可以拥有的最近的是IntStream,其中数组中的每个字节都被提升为int.

AFAIK,从字节数组构建一个的最简单方法是

    IntStream.Builder builder = IntStream.builder();
    for (byte b : byteArray) {
        builder.accept(b);
    }
    IntStream stream = builder.build();
Run Code Online (Sandbox Code Playgroud)

编辑:assylias建议另一种更短的方式:

IntStream stream = IntStream.range(0, byteArr.length)
                            .map(i -> byteArray[i]);
Run Code Online (Sandbox Code Playgroud)

  • 你也可以使用`IntStream.range(0,byteArr.length).map(i - > byteArr [i])` (5认同)