associateByTo
所在位置是kotlin.collections.associateByTo
,其相关用法介绍如下。
用法一
inline fun <T, K, M : MutableMap<in K, in T>> Array<out T>.associateByTo(
destination: M,
keySelector: (T) -> K
): M
inline fun <K, M : MutableMap<in K, in Byte>> ByteArray.associateByTo(
destination: M,
keySelector: (Byte) -> K
): M
inline fun <K, M : MutableMap<in K, in Short>> ShortArray.associateByTo(
destination: M,
keySelector: (Short) -> K
): M
inline fun <K, M : MutableMap<in K, in Int>> IntArray.associateByTo(
destination: M,
keySelector: (Int) -> K
): M
inline fun <K, M : MutableMap<in K, in Long>> LongArray.associateByTo(
destination: M,
keySelector: (Long) -> K
): M
inline fun <K, M : MutableMap<in K, in Float>> FloatArray.associateByTo(
destination: M,
keySelector: (Float) -> K
): M
inline fun <K, M : MutableMap<in K, in Double>> DoubleArray.associateByTo(
destination: M,
keySelector: (Double) -> K
): M
inline fun <K, M : MutableMap<in K, in Boolean>> BooleanArray.associateByTo(
destination: M,
keySelector: (Boolean) -> K
): M
inline fun <K, M : MutableMap<in K, in Char>> CharArray.associateByTo(
destination: M,
keySelector: (Char) -> K
): M
使用键值对填充并返回 destination 可变映射,其中键由应用于给定数组的每个元素的 keySelector 函数提供,值是元素本身。
如果任意两个元素具有由keySelector 返回的相同键,则将最后一个元素添加到Map中。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val charCodes = intArrayOf(72, 69, 76, 76, 79)
val byChar = mutableMapOf<Char, Int>()
println("byChar.isEmpty() is ${byChar.isEmpty()}") // true
charCodes.associateByTo(byChar) { Char(it) }
println("byChar.isNotEmpty() is ${byChar.isNotEmpty()}") // true
// L=76 only occurs once because only the last pair with the same key gets added
println(byChar) // {H=72, E=69, L=76, O=79}
//sampleEnd
}
输出:
byChar.isEmpty() is true byChar.isNotEmpty() is true {H=72, E=69, L=76, O=79}
用法二
inline fun <T, K, V, M : MutableMap<in K, in V>> Array<out T>.associateByTo(
destination: M,
keySelector: (T) -> K,
valueTransform: (T) -> V
): M
inline fun <K, V, M : MutableMap<in K, in V>> ByteArray.associateByTo(
destination: M,
keySelector: (Byte) -> K,
valueTransform: (Byte) -> V
): M
inline fun <K, V, M : MutableMap<in K, in V>> ShortArray.associateByTo(
destination: M,
keySelector: (Short) -> K,
valueTransform: (Short) -> V
): M
inline fun <K, V, M : MutableMap<in K, in V>> IntArray.associateByTo(
destination: M,
keySelector: (Int) -> K,
valueTransform: (Int) -> V
): M
inline fun <K, V, M : MutableMap<in K, in V>> LongArray.associateByTo(
destination: M,
keySelector: (Long) -> K,
valueTransform: (Long) -> V
): M
inline fun <K, V, M : MutableMap<in K, in V>> FloatArray.associateByTo(
destination: M,
keySelector: (Float) -> K,
valueTransform: (Float) -> V
): M
inline fun <K, V, M : MutableMap<in K, in V>> DoubleArray.associateByTo(
destination: M,
keySelector: (Double) -> K,
valueTransform: (Double) -> V
): M
inline fun <K, V, M : MutableMap<in K, in V>> BooleanArray.associateByTo(
destination: M,
keySelector: (Boolean) -> K,
valueTransform: (Boolean) -> V
): M
inline fun <K, V, M : MutableMap<in K, in V>> CharArray.associateByTo(
destination: M,
keySelector: (Char) -> K,
valueTransform: (Char) -> V
): M
使用键值对填充并返回 destination 可变映射,其中键由 keySelector 函数提供,值由应用于给定数组元素的 valueTransform 函数提供。
如果任意两个元素具有由keySelector 返回的相同键,则将最后一个元素添加到Map中。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val charCodes = intArrayOf(65, 65, 66, 67, 68, 69)
val byUpperCase = mutableMapOf<Char, Char>()
charCodes.associateByTo(byUpperCase, { Char(it) }, { Char(it + 32) })
// A=a only occurs once because only the last pair with the same key gets added
println(byUpperCase) // {A=a, B=b, C=c, D=d, E=e}
//sampleEnd
}
输出:
{A=a, B=b, C=c, D=d, E=e}
用法三
inline fun <T, K, M : MutableMap<in K, in T>> Iterable<T>.associateByTo(
destination: M,
keySelector: (T) -> K
): M
使用键值对填充并返回 destination 可变映射,其中键由应用于给定集合的每个元素的 keySelector 函数提供,值是元素本身。
如果任意两个元素具有由keySelector 返回的相同键,则将最后一个元素添加到Map中。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
data class Person(val firstName: String, val lastName: String) {
override fun toString(): String = "$firstName $lastName"
}
val scientists = listOf(Person("Grace", "Hopper"), Person("Jacob", "Bernoulli"), Person("Johann", "Bernoulli"))
val byLastName = mutableMapOf<String, Person>()
println("byLastName.isEmpty() is ${byLastName.isEmpty()}") // true
scientists.associateByTo(byLastName) { it.lastName }
println("byLastName.isNotEmpty() is ${byLastName.isNotEmpty()}") // true
// Jacob Bernoulli does not occur in the map because only the last pair with the same key gets added
println(byLastName) // {Hopper=Grace Hopper, Bernoulli=Johann Bernoulli}
//sampleEnd
}
输出:
byLastName.isEmpty() is true byLastName.isNotEmpty() is true {Hopper=Grace Hopper, Bernoulli=Johann Bernoulli}
用法四
inline fun <T, K, V, M : MutableMap<in K, in V>> Iterable<T>.associateByTo(
destination: M,
keySelector: (T) -> K,
valueTransform: (T) -> V
): M
使用键值对填充并返回 destination 可变映射,其中键由 keySelector 函数提供,值由应用于给定集合元素的 valueTransform 函数提供。
如果任意两个元素具有由keySelector 返回的相同键,则将最后一个元素添加到Map中。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
data class Person(val firstName: String, val lastName: String)
val scientists = listOf(Person("Grace", "Hopper"), Person("Jacob", "Bernoulli"), Person("Johann", "Bernoulli"))
val byLastName = mutableMapOf<String, String>()
println("byLastName.isEmpty() is ${byLastName.isEmpty()}") // true
scientists.associateByTo(byLastName, { it.lastName }, { it.firstName} )
println("byLastName.isNotEmpty() is ${byLastName.isNotEmpty()}") // true
// Jacob Bernoulli does not occur in the map because only the last pair with the same key gets added
println(byLastName) // {Hopper=Grace, Bernoulli=Johann}
//sampleEnd
}
输出:
byLastName.isEmpty() is true byLastName.isNotEmpty() is true {Hopper=Grace, Bernoulli=Johann}
相关用法
- 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用法及代码示例
- Kotlin ifBlank用法及代码示例
注:本文由纯净天空筛选整理自kotlinlang.org大神的英文原创作品 kotlin.collections.associateByTo。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。