Does anyone know why map on arrays returns a list ...
# announcements
j
Does anyone know why map on arrays returns a list and not an array?
d
map
always returns a
List
, regardless of input.
map
on a
Set
also returns a
List.
đź‘Ť 2
s
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
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
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!?)