associateByTo
所在位置是kotlin.text.associateByTo
,其相关用法介绍如下。
用法一
inline fun <K, M : MutableMap<in K, in Char>> CharSequence.associateByTo(
destination: M,
keySelector: (Char) -> K
): M
使用键值对填充并返回 destination 可变映射,其中键由应用于给定字符序列的每个字符的 keySelector 函数提供,值是字符本身。
如果任意两个字符具有由keySelector 返回的相同键,则最后一个将添加到Map中。
例子:
import java.util.Locale
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val string = "bonne journée"
// associate each character by its code
val result = mutableMapOf<Int, Char>()
string.associateByTo(result) { char -> char.code }
// notice each char code occurs only once
println(result) // {98=b, 111=o, 110=n, 101=e, 32= , 106=j, 117=u, 114=r, 233=é}
//sampleEnd
}
输出:
{98=b, 111=o, 110=n, 101=e, 32= , 106=j, 117=u, 114=r, 233=é}
用法二
inline fun <K, V, M : MutableMap<in K, in V>> CharSequence.associateByTo(
destination: M,
keySelector: (Char) -> K,
valueTransform: (Char) -> V
): M
使用键值对填充并返回destination 可变映射,其中键由keySelector 函数提供,值由应用于给定字符序列的字符的valueTransform 函数提供。
如果任意两个字符具有由keySelector 返回的相同键,则最后一个将添加到Map中。
例子:
import java.util.Locale
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val string = "bonne journée"
// associate each character by the code of its upper case equivalent and transform the character to upper case
val result = mutableMapOf<Int, Char>()
string.associateByTo(result, { char -> char.uppercaseChar().code }, { char -> char.uppercaseChar() })
// notice each char code occurs only once
println(result) // {66=B, 79=O, 78=N, 69=E, 32= , 74=J, 85=U, 82=R, 201=É}
//sampleEnd
}
输出:
{66=B, 79=O, 78=N, 69=E, 32= , 74=J, 85=U, 82=R, 201=É}
相关用法
- Kotlin associateByTo用法及代码示例
- Kotlin associateBy用法及代码示例
- Kotlin associateWithTo用法及代码示例
- Kotlin associate用法及代码示例
- Kotlin associateTo用法及代码示例
- Kotlin associateWith用法及代码示例
- 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.associateByTo。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。