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

You can do this with find, which gives you the first element of a list that matches a given predicate (or null, if none matched):

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

Or if you really do need the last element that matches the predicate, as your Java example code does, you can use last:

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)