Search code examples
listkotlingenericscollectionsmaps

Iterate with List(mylist.size){ index -> TODO()} or Map in kotlin Kotlin


I found this code and I would like know which is more efficient to big lists of elements.

return List(myBigList.size){ index ->
   val element = myBigList[index]
   myConversionFunction(element,param2,param3,param4)
}

I though this code could be replaced by

return myBigList.map{
    myConversionFunction(it,param2,param3,param4)
}

Which way is better if I have a lot of elements to iterate, or you suggest other one?


Solution

  • Both options are just as efficient, you should use the clearest one, which is the second option.