associateWithTo
所在位置是kotlin.text.associateWithTo
,其相關用法介紹如下。
用法:
inline fun <V, M : MutableMap<in Char, in V>> CharSequence.associateWithTo(
destination: M,
valueSelector: (Char) -> V
): M
使用給定字符序列的每個字符的鍵值對填充並返回 destination 可變映射,其中鍵是字符本身,值由應用於該鍵的 valueSelector 函數提供。
如果任何兩個字符相等,則最後一個字符將覆蓋映射中的前一個值。
例子:
import java.util.Locale
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val string = "bonne journée"
// associate each character with its code
val result = mutableMapOf<Char, Int>()
string.associateWithTo(result) { char -> char.code }
// notice each letter occurs only once
println(result) // {b=98, o=111, n=110, e=101, =32, j=106, u=117, r=114, é=233}
//sampleEnd
}
輸出:
{b=98, o=111, n=110, e=101, =32, j=106, u=117, r=114, é=233}
相關用法
- Kotlin associateWithTo用法及代碼示例
- Kotlin associateWith用法及代碼示例
- Kotlin associateBy用法及代碼示例
- Kotlin associate用法及代碼示例
- Kotlin associateTo用法及代碼示例
- Kotlin associateByTo用法及代碼示例
- Kotlin asSequence用法及代碼示例
- Kotlin asReversed用法及代碼示例
- Kotlin all用法及代碼示例
- Kotlin any用法及代碼示例
- Kotlin aggregateTo用法及代碼示例
- Kotlin aggregate用法及代碼示例
- Kotlin map用法及代碼示例
- Kotlin filterNot用法及代碼示例
- Kotlin reduceRight用法及代碼示例
- Kotlin Random.Default用法及代碼示例
- Kotlin Byte.inc用法及代碼示例
- Kotlin getValue用法及代碼示例
- Kotlin Double.dec用法及代碼示例
- Kotlin windowedSequence用法及代碼示例
- Kotlin contentToString用法及代碼示例
- Kotlin groupByTo用法及代碼示例
- Kotlin commonPrefixWith用法及代碼示例
- Kotlin MatchResult.Destructured用法及代碼示例
- Kotlin Delegates.notNull用法及代碼示例
注:本文由純淨天空篩選整理自kotlinlang.org大神的英文原創作品 kotlin.text.associateWithTo。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。