https://kotlinlang.org logo
#announcements
Title
# announcements
j

Jamie Taylor

11/21/2019, 4:18 PM
Does anyone know why map on arrays returns a list and not an array?
d

diesieben07

11/21/2019, 4:18 PM
map
always returns a
List
, regardless of input.
map
on a
Set
also returns a
List.
👍 2
s

Stephan Schroeder

11/21/2019, 4:40 PM
For
Set
it’s probably forced because multiple values could be mapped to the same one and they didn’t want to have the number of elements change within a
map
operation. On Array, it’s probably to avoid to have to reimplement all the operations present on Iterable???
m

Mtsahakis

11/21/2019, 5:05 PM
I guess they could have implemented specific methods for each use case and not a general
Iterable
one but that ship might have sailed as other parts of the api could expect map to return a
List
I was scratching my head when after a
map
operation I had to convert back to an array for my use case.
s

Stephan Schroeder

11/21/2019, 5:11 PM
you have toXArray() on Collection for all primitive Java types (
X
= Boolean, Char and all the number types), so you can easily repackage the result of your map-operation in a new array (I guess normal objects shouldn’t be packed into arrays anyway!?)
2 Views