Kotlin how to return a SINGLE object from a list that contains a specific id?

后端 未结 4 1282
迷失自我
迷失自我 2020-12-31 01:58

Good day, i\'m stuck figuring out how to get a single object from a list, i did google but all the topics show how to return a List with sorted objects or somet

4条回答
  •  死守一世寂寞
    2020-12-31 02:59

    Simplify

    val user: User = myList.single { it.userId == id }
    

    or if may list not have your filter

    val user: User? = myList.singleOrNull{ it.userId == id }
    

提交回复
热议问题