generics - How to use Java Stream map for mapping between different types? -


i have 2 arrays of equal size:

  1. int[] permutation
  2. 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

Popular posts from this blog

python - jinja2: TemplateSyntaxError: expected token ',', got 'string' -

Qt4: how to send QString inside a struct via QSharedMemory -

node.js - NodeJS remote terminal to Dropbear OpenWRT-Server -