generics - How to use Java Stream map for mapping between different types? -
i have 2 arrays of equal size:
int[] permutation
t[] source
i want smth this
arrays.stream(permutation).map(i -> source[i]).toarray();
but won't work saying: incompatible types: bad return type in lambda expression
arrays.stream
int[]
give intstream
map
expect intunaryoperator
(a function int -> int
).
the function provide of type int -> t
t object (it work if t
integer
due unboxing, not unbounded generic type parameter, assuming it's generic type).
what looking use maptoobj
instead, expects intfunction
(a function int -> t
) , gives stream<t>
:
//you might want use overloaded toarray() method also. arrays.stream(permutation).maptoobj(i -> source[i]).toarray();
Comments
Post a Comment