While playing around with Java8's Streams-API, I stumbled over the following:
To convert an array of primitive wrapper classe objects into a Stream
I just have to call Stream.of(array)
. But to convert an array of primitive data types, I have to call .of(array)
from the corresponding wrapper (class) stream class (<-- that sounds silly).
An example:
final Integer[] integers = {1, 2, 3};
final int[] ints = {1, 2, 3};
Stream.of(integers).forEach(System.out::println); //That works just fine
Stream.of(ints).forEach(System.out::println); //That doesn't
IntStream.of(ints).forEach(System.out::println); //Have to use IntStream instead
My question(s):
Why is this? Does this correlate to e.g. the behaviour of Arrays.asList()
which also just works for wrapper class arrays?
Arrays.stream(ints).forEach(System.out::println)
. – Humperdinck