how to get single field value in object list

13,901

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 }

Share:
13,901
Taehyung Kim
Author by

Taehyung Kim

Updated on June 03, 2022

Comments

  • Taehyung Kim
    Taehyung Kim almost 2 years

    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"}