Wesley Hartford
05/05/2022, 7:43 PMMap.computeIfAbsent()
function documentation includes:
However, when calling this function from Kotlin code, the mapping function has a signatureCopy codeIf the mapping function returns {@code null}, no mapping is recorded.
(K) -> V
, meaning that the passed in function cannot return null
. Is there any way to use computeIfAbsent
from kotlin passing a function which may return null
?Klitos Kyriacou
05/06/2022, 8:16 AM(mutableMapOf(1 to "one") as MutableMap<Int, String?>).computeIfAbsent(1) { null }
Jordan Stewart
05/06/2022, 8:40 AMnull
for compute
and computeIfPresent
. Why the difference? the only difference in signature is that they're BiFunction
rather than Function
Klitos Kyriacou
05/06/2022, 8:58 AMWesley Hartford
05/06/2022, 2:59 PMcompute
function.dmcg
05/06/2022, 3:04 PM