when i use Java 8 Stream.of primitive type, the result is confused

后端 未结 2 457
走了就别回头了
走了就别回头了 2020-12-11 07:11
    byte[] a = {1,2,3};
    System.out.println(Stream.of(a).count());

    Byte[] b = {1,2,3};
    System.out.println(Stream.of(b).count());

the re

2条回答
  •  忘掉有多难
    2020-12-11 07:58

    Stream.of only accepts Objects as its arguments. A byte isn't an Object, but a byte array is. If a is an array of byte, then Stream.of(a) can only mean "stream of this one object, which is an array".

    If you have a Byte[] array, then each element of the array is an object, so the compiler can guess that's what you mean.

    There is information here about how you can stream a byte array: In Java 8, is there a ByteStream class?

提交回复
热议问题