33

how to get some field list in object list in kotlin

fun main(args:Array<String>){
    println("Hello World")
    val list = listOf(member("hong",10), member("kil", 10))
}

data class member(var name:String, var age:Int)

Above code, I want to get single field list in above code such as name

  {"hong", "kil"}
0

1 Answer 1

88

You want to get list with only name field? If so, then use map (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/map.html)

val names = list.map { it.name }

2
  • wow. It is simple solution and works very well. thanks. Commented May 30, 2018 at 11:46
  • man, do i really have to read through all the documentation to find this gem? Commented Jul 26, 2019 at 9:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.