Search code examples
listkotlinobjectforeachentity

Kotlin List Objects to List Id's


I have a list of objects Human. I need to get a list of its id. I do that. Can i do it more simplier? It looks not so good

  val idsList = ArrayList<Int>()
humansList.forEach{idsList.add(it.id)}

Solution

  • You can simply map ids from humanList to idsList

    val idsList = humalList.map { it.id }