genericskotlinextension-function

Filter null keys and values from map in Kotlin


I have an extension function that filters away entries with null keys or values:

fun <K, V> Map<K?, V?>.filterNotNull(): Map<K, V> = this.mapNotNull { 
   it.key?.let { key -> 
      it.value?.let { value -> 
         key to value 
      }
   }
}.toMap()

This works for a map with nullable keys and values:

data class Stuff(val foo: Int)

val nullMap = mapOf<String?, Stuff?>(null to (Stuff(1)), "b" to null, "c" to Stuff(3))
assert(nullMap.filterNotNull().map { it.value.foo } == listOf(3))

But not in one that has non-nullable keys or values:

val nullValues = mapOf<String, Stuff?>("a" to null, "b" to Stuff(3))    
assert(nullValues.filterNotNull().map { it.value.foo } == listOf(3))

Type mismatch: inferred type is Map<String, Stuff?> but Map<String?, Stuff?> was expected
Type inference failed. Please try to specify type arguments explicitly.

Is there a way to make my extension function work for both cases, or do I need to provide two separate functions?


Solution

  • I will later figure out why, but adding out to the map is working:

    fun <K : Any, V : Any> Map<out K?, V?>.filterNotNull(): Map<K, V> = ...