Shorten filter and map in Kotlin

3393 views filter
2

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

8

You can use mapNotNull:

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

posted this

Have an answer?

JD

Please login first before posting an answer.