getOrElse
inline fun ByteArray.getOrElse(
index: Int,
defaultValue: (Int) -> Byte
): Byteinline fun ShortArray.getOrElse(
index: Int,
defaultValue: (Int) -> Short
): Shortinline fun IntArray.getOrElse(
index: Int,
defaultValue: (Int) -> Int
): Intinline fun LongArray.getOrElse(
index: Int,
defaultValue: (Int) -> Long
): Longinline fun FloatArray.getOrElse(
index: Int,
defaultValue: (Int) -> Float
): Floatinline fun DoubleArray.getOrElse(
index: Int,
defaultValue: (Int) -> Double
): Doubleinline fun BooleanArray.getOrElse(
index: Int,
defaultValue: (Int) -> Boolean
): Booleaninline fun CharArray.getOrElse(
index: Int,
defaultValue: (Int) -> Char
): Char@ExperimentalUnsignedTypes inline fun ULongArray.getOrElse(
index: Int,
defaultValue: (Int) -> ULong
): ULong@ExperimentalUnsignedTypes inline fun UByteArray.getOrElse(
index: Int,
defaultValue: (Int) -> UByte
): UByte@ExperimentalUnsignedTypes inline fun UShortArray.getOrElse(
index: Int,
defaultValue: (Int) -> UShort
): UShortReturns an element at the given index or the result of calling the defaultValue function if the index is out of bounds of this array.
inline fun <T> List<T>.getOrElse(
index: Int,
defaultValue: (Int) -> T
): TReturns an element at the given index or the result of calling the defaultValue function if the index is out of bounds of this list.
inline fun <K, V> Map<K, V>.getOrElse(
key: K,
defaultValue: () -> V
): VReturns the value for the given key, or the result of the defaultValue function if there was no entry for the given key.
import kotlin.test.*
import java.util.*
fun main(args: Array<String>) {
//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
}