Uziel Sulkies
09/18/2019, 7:14 PMShawn
09/18/2019, 7:16 PMfilterNot
will do what you want it toV
as V : Any
streetsofboston
09/18/2019, 7:16 PMthis.filterNot { it.value == null }.map { it as Entry<K,V> }
(not sure if Entry
is the correct name)this.filterNot { it.value == null } as Map<K,V>
Kroppeb
09/18/2019, 7:20 PMUziel Sulkies
09/18/2019, 7:23 PMstreetsofboston
09/18/2019, 7:25 PMfun <K, V : Any> Map<K,V?>.filterNotNull(): Map<K,V> =
filter { it.value != null } as Map<K,V>
Shawn A
09/18/2019, 7:40 PMShawn
09/18/2019, 7:43 PMmapNotNull
fun <K : Any, V : Any> Map<K, V?>.filterNullValues(): Map<K, V> {
return mapNotNull { it.value?.let { value -> it.key to value } }.toMap()
}
mapNotNull
returns List<R>
Map<K, V>
Shawn A
09/18/2019, 7:47 PMShawn
09/18/2019, 7:48 PMmapNotNull
route involves different tradeoffs, mostly just more iteration over your collection