getOrElseIfNull

inline fun <K, V> Map<K, V>.getOrElseIfNull(key: K, defaultValue: () -> V): V(source)

Returns the value for the given key if the value is present and not null. Otherwise, returns the result of the defaultValue function.

In contrast to getOrElseIfMissing, this function returns the result of the defaultValue function if the key is mapped to a null value.

Since Kotlin

2.4

Samples

import kotlin.test.*
import java.util.*

fun main() { 
   //sampleStart 
   val map = mutableMapOf<String, Int?>()
println(map.getOrElseIfNull("x") { 1 }) // 1

map["x"] = 3
println(map.getOrElseIfNull("x") { 1 }) // 3

map["x"] = null
println(map.getOrElseIfNull("x") { 1 }) // 1 
   //sampleEnd
}