get Or Else
Returns an element at the given index or the result of calling the defaultValue function if the index is out of bounds of this array.
Returns an element at the given index or the result of calling the defaultValue function if the index is out of bounds of this list.
Returns an element at the given index or the result of calling the defaultValue function if the index is out of bounds of this array.
Since Kotlin
1.3Returns the value for the given key, or the result of the defaultValue function if there was no entry for the given key.
Samples
import kotlin.test.*
import java.util.*
fun main() {
//sampleStart
val map = mutableMapOf<String, Int?>()
println(map.getOrElse("x") { 1 }) // 1
map["x"] = 3
println(map.getOrElse("x") { 1 }) // 3
map["x"] = null
println(map.getOrElse("x") { 1 }) // 1
//sampleEnd
}