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 }