Nikky
11/22/2020, 3:11 AMval filteredMap: Map<String, B> = someMap.filterValues { value -> value is B } as Map<String, B>
because the unchecked cast is marked as warningnanodeath
11/22/2020, 3:33 AMfilterValuesInstanceOf
method and suppress it there.Nir
11/22/2020, 5:15 AMNir
11/22/2020, 5:15 AMNir
11/22/2020, 5:15 AMNir
11/22/2020, 5:16 AMNir
11/22/2020, 5:19 AMNir
11/22/2020, 5:21 AMNir
11/22/2020, 5:21 AMNikky
11/22/2020, 11:34 AMinline fun <K, reified RV> Map<K, *>.filterValueIsInstance(): Map<K, RV> {
@OptIn(ExperimentalStdlibApi::class)
return buildMap {
this@filterValueIsInstance.filterValueIsInstanceTo(this)
}
}
inline fun <reified RK: K, reified RV: V, K, V, C : MutableMap<in RK, in RV>> Map<K, V>.filterIstInstance(destination: C): C {
for ((key, value) in this) if (key is RK && value is RV) destination.put(key, value)
return destination
}
inline fun <reified RV: V, K, V, C : MutableMap<K, in RV>> Map<K, *>.filterValueIsInstanceTo(destination: C): C {
for ((key, value) in this) if (value is RV) destination.put(key, value)
return destination
}
inline fun <reified RK: K, K, V, C : MutableMap<in RK, V>> Map<*, V>.filterKeyIsInstance(destination: C): C {
for ((key, value) in this) if (key is RK) destination.put(key, value)
return destination
}
Nikky
11/22/2020, 11:35 AMVampire
11/23/2020, 2:52 AMval filteredMap: Map<String, B> = someMap.mapNotNull { (key, value) -> if (value is B) key to value else null }.toMap()