getOrElse

inline fun <T> Array<out T>.getOrElse(index: Int, defaultValue: (Int) -> T): T(source)
inline fun ByteArray.getOrElse(index: Int, defaultValue: (Int) -> Byte): Byte(source)
inline fun ShortArray.getOrElse(index: Int, defaultValue: (Int) -> Short): Short(source)
inline fun IntArray.getOrElse(index: Int, defaultValue: (Int) -> Int): Int(source)
inline fun LongArray.getOrElse(index: Int, defaultValue: (Int) -> Long): Long(source)
inline fun FloatArray.getOrElse(index: Int, defaultValue: (Int) -> Float): Float(source)
inline fun DoubleArray.getOrElse(index: Int, defaultValue: (Int) -> Double): Double(source)
inline fun BooleanArray.getOrElse(index: Int, defaultValue: (Int) -> Boolean): Boolean(source)
inline fun CharArray.getOrElse(index: Int, defaultValue: (Int) -> Char): Char(source)

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.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val emptyArray: Array<Any> = emptyArray()
println(emptyArray.getOrElse(0) { "default" }) // default

val array = arrayOf(1)
println(array.getOrElse(0) { 0 }) // 1
println(array.getOrElse(-1) { 0 }) // 0
println(array.getOrElse(0) { "default" }) // 1
println(array.getOrElse(-1) { "default" }) // default

// arrays of primitive types
val intArray = intArrayOf(1, 2, 3)
println(intArray.getOrElse(0) { 0 }) // 1
println(intArray.getOrElse(-1) { 0 }) // 0

val booleanArray = booleanArrayOf(true, false)
println(booleanArray.getOrElse(0) { false }) // true
println(booleanArray.getOrElse(-1) { false }) // false

val charArray = charArrayOf('a', 'b', 'c')
println(charArray.getOrElse(0) { 'z' }) // a
println(charArray.getOrElse(-1) { 'z' }) // z

// arrays of unsigned types
val uIntArray = uintArrayOf(1u, 2u, 3u)
println(uIntArray.getOrElse(0) { 10u }) // 1
println(uIntArray.getOrElse(-1) { 10u }) // 10 
   //sampleEnd
}

inline fun <T> List<T>.getOrElse(index: Int, defaultValue: (Int) -> T): T(source)

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.

Since Kotlin

1.0

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3)
println(list.getOrElse(0) { 42 }) // 1
println(list.getOrElse(2) { 42 }) // 3
println(list.getOrElse(3) { 42 }) // 42
println(list.getOrElse(-1) { 42 }) // 42

val emptyList = emptyList<Int>()
println(emptyList.getOrElse(0) { "no int" }) // no int 
   //sampleEnd
}

inline fun UIntArray.getOrElse(index: Int, defaultValue: (Int) -> UInt): UInt(source)
inline fun ULongArray.getOrElse(index: Int, defaultValue: (Int) -> ULong): ULong(source)
inline fun UByteArray.getOrElse(index: Int, defaultValue: (Int) -> UByte): UByte(source)
inline fun UShortArray.getOrElse(index: Int, defaultValue: (Int) -> UShort): UShort(source)

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.3

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val emptyArray: Array<Any> = emptyArray()
println(emptyArray.getOrElse(0) { "default" }) // default

val array = arrayOf(1)
println(array.getOrElse(0) { 0 }) // 1
println(array.getOrElse(-1) { 0 }) // 0
println(array.getOrElse(0) { "default" }) // 1
println(array.getOrElse(-1) { "default" }) // default

// arrays of primitive types
val intArray = intArrayOf(1, 2, 3)
println(intArray.getOrElse(0) { 0 }) // 1
println(intArray.getOrElse(-1) { 0 }) // 0

val booleanArray = booleanArrayOf(true, false)
println(booleanArray.getOrElse(0) { false }) // true
println(booleanArray.getOrElse(-1) { false }) // false

val charArray = charArrayOf('a', 'b', 'c')
println(charArray.getOrElse(0) { 'z' }) // a
println(charArray.getOrElse(-1) { 'z' }) // z

// arrays of unsigned types
val uIntArray = uintArrayOf(1u, 2u, 3u)
println(uIntArray.getOrElse(0) { 10u }) // 1
println(uIntArray.getOrElse(-1) { 10u }) // 10 
   //sampleEnd
}

inline fun <K, V> Map<K, V>.getOrElse(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.

Since Kotlin

1.0

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
}