I'd like to find the clearest and most elegant way to convert a Map<String, String?> to a Map<String, String>, filtering out the pairs with null values.
I have a contrived solution below, but I don't like how I have to do an unsafe !!. Is there a better way to do this?
fun Map<String, String?>.filterNonNull() = this
.filter { it.value != null }
.map { it.key to it.value!! }
.toMap()
!!in this particular situation: it's pretty simple function, and it's clear that there is no NPE. So I like your solution (it can be simplified/optimized by using kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/…)Map<..., ...?>in the first place. When I writeyour_map[k]and receivenull, I would usually expect it to means that the key is not present in the map. Maybe it's possible to avoid creating such map at all?