poohbar
07/02/2019, 7:07 PMlist.filter { it.field != null }
.associateBy({ it.id }, { it.field!! })
is there a way to avoid !!
on the second line?
list
is a list of:
data class Person(val id: String, val field: Field?)
Dico
07/02/2019, 7:13 PMmapNotNull
to Pair?
and then invoke .toMap()
Pavlo Liapota
07/02/2019, 7:14 PMcolumns
.mapNotNull { (id, direction) ->
if (direction == null) null else id to direction
}
.toMap()
Dico
07/02/2019, 7:15 PM(id, dir) -> dir?.let { Pair(id, it) }
Pavlo Liapota
07/02/2019, 7:15 PMDico
07/02/2019, 7:17 PMkarelpeeters
07/02/2019, 7:21 PMmap.get
a missing entry and a null value behave the same.Pavlo Liapota
07/02/2019, 7:25 PMassociate
instead of associateBy
in such cases:
.associate { it.id to it.direction!! }
poohbar
07/02/2019, 7:27 PM