Integer stream

I have a code:

int[] values = { 1, 4, 9, 16 };
Stream<Integer> ints = Stream.of(values);

which gives me a compilation error. But:

int[] values = { 1, 4, 9, 16 };
Stream<Integer> ints = Stream.of(new Integer[] {1, 4, 9, 16});

does not give so. Why?

+6
source share
3 answers

In the first example, you pass an array of primitives intto Stream#of, which can take either an object or an array of objects. Primitives are not objects.

In the second example, it compiles because you are passing an array from Integer.

You can use IntStream#ofthat accept arrays int.

+12
source

Because int[]and Integer[]- different types. The first is an array of primitives, the second is an array of objects with a type Integer.

You can use IntStream.of(int[])orStream.of(Integer[])

+4
source

, :

Arrays.stream(values).boxed();
+4

Source: https://habr.com/ru/post/1014522/


All Articles