dave08
11/15/2018, 5:38 PMassociateWithNotNull { }
for when the value is resolved to be null
, just like mapNotNull { }
?dave08
11/15/2018, 5:48 PMinline fun <K, V> Iterable<K>.associateWithNotNull(valueSelector: (K) -> V?): Map<K, V> {
val result = LinkedHashMap<K, V>(mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16))
return associateWithToNotNull(result, valueSelector)
}
inline fun <K, V, M : MutableMap<in K, in V>> Iterable<K>.associateWithToNotNull(destination: M, valueSelector: (K) -> V?): M {
for (element in this) {
val value = valueSelector(element)
if (value != null) destination.put(element, value)
}
return destination
}
dave08
11/15/2018, 5:56 PM