getOrElseIfMissing
Returns the value for the given key if the value is present. Otherwise, returns the result of the defaultValue function.
In contrast to getOrElseIfNull, this function returns the mapped value, even if that value is null.
Note that the operation is not guaranteed to be atomic if the map is being modified concurrently.
Since Kotlin
2.4Samples
import kotlin.test.*
import java.util.*
fun main() {
//sampleStart
val map = mutableMapOf<String, Int?>()
println(map.getOrElseIfMissing("x") { 1 }) // 1
map["x"] = 3
println(map.getOrElseIfMissing("x") { 1 }) // 3
map["x"] = null
println(map.getOrElseIfMissing("x") { 1 }) // null
//sampleEnd
}Returns the value for the given key if the value is present. Otherwise, returns the result of the defaultValue function.
In contrast to getOrElseIfNull, this function returns the mapped value, even if that value is null.
This function is specialized for maps that can be modified concurrently to obtain the value for the given key and check its presence in the map atomically.
Since Kotlin
2.4Samples
import kotlin.test.*
import java.util.*
fun main() {
//sampleStart
val map = mutableMapOf<String, Int?>()
println(map.getOrElseIfMissing("x") { 1 }) // 1
map["x"] = 3
println(map.getOrElseIfMissing("x") { 1 }) // 3
map["x"] = null
println(map.getOrElseIfMissing("x") { 1 }) // null
//sampleEnd
}