I have the code:
int[] values = { 1, 4, 9, 16 };
Stream<Integer> ints = Stream.of(values);
which gives me compilation error. But:
int[] values = { 1, 4, 9, 16 };
Stream<Integer> ints = Stream.of(new Integer[] {1, 4, 9, 16});
doesn't give so. Why?
An IntStream interface extends the BaseStream interface in Java 8. It is a sequence of primitive int-value elements and a specialized stream for manipulating int values. We can also use the IntStream interface to iterate the elements of a collection in lambda expressions and method references.
Stream<Integer> operates on boxed values ( Integer instead of primitive int) which takes significantly more memory and usually a lot of boxing/unboxing operations (depending on your code), whereas IntStream works with primitives.
The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value and the median is the mean of the two middle values. For example, for arr = [2,3,4] , the median is 3 . For example, for arr = [2,3] , the median is (2 + 3) / 2 = 2.5 .
In the first example, you are passing an array of primitives int
s to Stream#of
which can take either an object, or an array of object. Primitives are not objects.
In the second example, it compiles becauses you pass in an array of Integer
.
You can use IntStream#of
which accept int
arrays.
Because int[]
and Integer[]
is different types. First one is array of primitives, second one is array of objects with type Integer
.
You can use IntStream.of(int[])
or Stream.of(Integer[])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With