Is there a way to filter out null Any? values in Kotlin Map?
Asked Answered
I

4

25

I'm trying to think of a function that would allow a Map<String, Any?> object to be treated as Map<String,Any> through type inference through applying a single function.

I am pretty new to the transformation functions in Kotlin and have tried the various filter and filterValues filterNot on the map like so:

val input = mapOf(Pair("first",null))
val filtered: Map<String,Any> = input.filter { it.value!=null }

it also fails to compile with any of these

input.filterValues { it!=null }
input.filterNot { it.value==null }
input.filterNot { it.value is Nothing }

The closest I can seem to get is applying multiple steps or having an Unchecked cast warning. I would have thought that filtering the values to be !=null would suffice. My only other thought is that it's due to the generics?

Idolize answered 18/2, 2017 at 8:17 Comment(0)
B
25

The filter functions return a Map with the same generic types as the original map. To transform the type of the value, you need to map the values from Any? to Any, by doing a cast. The compiler can't know that the predicate you pass to filter() makes sure all the values of the filtered map are non-null, so it can't use type inference. So your best et is to use

val filtered: Map<String, Any> = map.filterValues { it != null }.mapValues { it -> it.value as Any }

or to define a function doing the filtering and the transformation in a single pass, and thus be able to use smart casts:

fun filterNotNullValues(map: Map<String, Any?>): Map<String, Any> {
    val result = LinkedHashMap<String, Any>()
    for ((key, value) in map) {
        if (value != null) result[key] = value
    }
    return result
}
Baliol answered 18/2, 2017 at 8:52 Comment(7)
can the call to mapValues be simplified or are we stuck with taking in the Map.Entry? It seems weird that it wouldn't just provide the value of type V and expect a return of type R to me but I suppose that would incur a performance cost that iterating over an entry set doesn't. Maybe the function name is too ambiguous?Idolize
I just realised that .mapValues { it.value as Any } achieves the same result, and removes passing the already accessible itIdolize
I don't think it has anything to do with performance. Whether the mapper function takes an entry or a value doesn't change anything: the mapValues() function still needs to iterate over all entries. Taking an entry rather than just a value allows doing more things than just taking the value, because you might need to have access to the key to decide how to transform the value.Baliol
yeah that's a good point. I actually decided this implementation was better from removing the Unchecked cast warning, and ended up just writing an extension function fun <K,V> Map<K,V?>.filterNullValues() = this.filterValues{it!=null}.mapValues { it.value as V } which seems to pass a trivial test I checked against.Idolize
It's less efficient than removing the warning, and removing the warning is safe in this case. If you really create your own function anyway, implementing it as I show in my answer would be more efficient than doing two passes on the map.Baliol
Right. That makes sense from the performance perspective. Kotlin's stream/iterate-like functions are still a bit lost on me being so new.Idolize
why don't you use extension?Said
P
20

The compiler just doesn't perform type analysis deep enough to infer that, for example, input.filterValues { it != null } filters out null values from the map and thus the resulting map should have a not-null value type. Basically there can be arbitrary predicate with arbitrary meaning in terms of types and nullability.

There is no special case function for filtering null values out of a map in the stdlib (like there is .filterIsInstance<T>() for iterables). Therefore your easiest solution is to apply an unchecked cast thus telling the compiler that you are sure about the type safety not being violated:

@Suppress("UNCHECKED_CAST")
fun <K, V> Map<K, V?>.filterNotNullValues() = filterValues { it != null } as Map<K, V>

See also: another question with a similar problem about is-check.

Phosphoric answered 18/2, 2017 at 8:49 Comment(1)
I like this function, it seems like it should be alongside the filterNotNull() for list / iterables though I'm not sure of how that would modify computation cost to apply the function in comparison to thoseIdolize
I
4

This yields no warnings kotlin 1.5.30

listOfNotNull(
        nullableString?.let { "key1" to it },
        nullableString?.let { "key2" to it }
    ).toMap()
Interlocutress answered 30/8, 2021 at 17:48 Comment(1)
It yields a warning from me, though: This creates a ton of new objects you don't need. The solution by @Phosphoric is much nicer and way more efficient.Nakisha
O
0

It's an extra step to map to a list and back to a map, but it gives you what you want:

val input = mapOf(Pair("first", null), Pair("second", "hello"))
val filtered: Map<String, Any> =
    input.filter { it.value != null }
        .map { it.key to it.value!! }
        .toMap()
Overcast answered 30/4 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.