getOrElse
Returns the value for the given key, or the result of the defaultValue function if there was no entry for the given key.
Since Kotlin
1.1Samples
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
}
Returns the value for the given key, or the result of the defaultValue function if there was no entry for the given key.
Since Kotlin
1.0Samples
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
}
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.0Returns an element at the given index or the result of calling the defaultValue function if the index is out of bounds of this list.