Mapping arrays to list of objects kotlin

Try Array.zip and then map:

val list = a.zip(b)
            .map { SomeClass(it.first, it.second) }

or if you like it more:

val list = a.zip(b)
            .map { (a, b) -> SomeClass(a, b) }

Note that if both arrays differ in size, the additional values are ignored. Note also that this will create intermediate Pairs (which is the default transformation function of zip). Even though I like the explicit map more, @hotkeys solution regarding the overloaded method is more appropriate (you spare that hidden Pair-transformation):

val list = a.zip(b) { a, b -> SomeClass(a, b) }

And where the overloaded method probably shines, is when using references instead:

a.zip(b, ::SomeClass)

Which will work as long as you have a constructor matching the zipped arguments and doesn't work out of the box for the Pair (yet?).


Improving on @Roland's answer, you can use the zip overload that accepts a two-argument function for mapping the pairs immediately:

val result = a.zip(b) { x, y -> SomeClass(x, y) }