Shorten filter and map in Kotlin

3393 views filter

From a list of nullable users I want the non-null IDs of non-null users. Is there a way to shorten this expression?

list.filter {t -> t.user!!.id !== null }.map { t -> t.user!!.id!! }

answered question

1 Answer


You can use mapNotNull:

list.mapNotNull { t -> t.user?.id }

posted this

Have an answer?


Please login first before posting an answer.