I put some numbers into a HashSet
, then try to get an int[]
using stream:
HashSet set = new HashSet<Integer>();
set.add(1);
set.add(2);
set.add(3);
int[] ii = set.stream().mapToInt(i -> ((Integer)i).intValue()).toArray();
It works fine. My question is why I need to cast i
into an Integer
for it to work? If I don't cast, i
stays an Object
.
According to the docs, stream()
should return a Stream<E>
, where E
in this case is Integer
. That is why I did not anticipate the cast.