getOrElse
所在位置是kotlin.collections.getOrElse
,其相關用法介紹如下。
用法一
@ExperimentalUnsignedTypes inline fun UIntArray.getOrElse(
index: Int,
defaultValue: (Int) -> UInt
): UInt
@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
): UShort
如果 index 超出此數組的範圍,則返回給定 index 處的元素或調用 defaultValue 函數的結果。
用法二
如果 index 超出此列表的範圍,則返回給定 index 處的元素或調用 defaultValue 函數的結果。
用法三
返回給定鍵的值,或者如果給定鍵沒有條目,則返回 defaultValue 函數的結果。
例子:
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
}
輸出:
1 3 1
相關用法
- Kotlin getOrPut用法及代碼示例
- Kotlin getOrNull用法及代碼示例
- Kotlin getValue用法及代碼示例
- Kotlin groupByTo用法及代碼示例
- Kotlin groupBy用法及代碼示例
- Kotlin groupingBy用法及代碼示例
- Kotlin associateBy用法及代碼示例
- Kotlin all用法及代碼示例
- Kotlin map用法及代碼示例
- Kotlin filterNot用法及代碼示例
- Kotlin reduceRight用法及代碼示例
- Kotlin Random.Default用法及代碼示例
- Kotlin Byte.inc用法及代碼示例
- Kotlin Double.dec用法及代碼示例
- Kotlin windowedSequence用法及代碼示例
- Kotlin contentToString用法及代碼示例
- Kotlin commonPrefixWith用法及代碼示例
- Kotlin MatchResult.Destructured用法及代碼示例
- Kotlin Delegates.notNull用法及代碼示例
- Kotlin ifBlank用法及代碼示例
- Kotlin filterNotTo用法及代碼示例
- Kotlin Triple.<init>用法及代碼示例
- Kotlin Duration.toString用法及代碼示例
- Kotlin windowed用法及代碼示例
- Kotlin indexOf用法及代碼示例
注:本文由純淨天空篩選整理自kotlinlang.org大神的英文原創作品 kotlin.collections.getOrElse。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。