You need to use the mapToLong
operation.
int[] intArray = {1, 2, 3};
long[] longArray = Arrays.stream(intArray).mapToLong(i -> i).toArray();
or, as Holger points out, in this case, you can directly use asLongStream()
:
int[] intArray = {1, 2, 3};
long[] longArray = Arrays.stream(intArray).asLongStream().toArray();
The map
method on primitive streams return a stream of the same primitive type. In this case, IntStream.map
will still return an IntStream
.
The cast to long
with
.map(item -> ((long) item))
will actually make the code not compile since the mapper used in IntStream.map
is expected to return an int
and you need an explicit cast to convert from the new casted long
to int
.
With .mapToLong(i -> i)
, which expects a mapper returning a long
, the int i
value is promoted to long
automatically, so you don't need a cast.