associateWithTo所在位置是kotlin.collections.associateWithTo,其相關用法介紹如下。

用法一

inline fun <K, V, M : MutableMap<in K, in V>> Array<out K>.associateWithTo(
    destination: M, 
    valueSelector: (K) -> V
): M
inline fun <V, M : MutableMap<in Byte, in V>> ByteArray.associateWithTo(
    destination: M, 
    valueSelector: (Byte) -> V
): M
inline fun <V, M : MutableMap<in Short, in V>> ShortArray.associateWithTo(
    destination: M, 
    valueSelector: (Short) -> V
): M
inline fun <V, M : MutableMap<in Int, in V>> IntArray.associateWithTo(
    destination: M, 
    valueSelector: (Int) -> V
): M
inline fun <V, M : MutableMap<in Long, in V>> LongArray.associateWithTo(
    destination: M, 
    valueSelector: (Long) -> V
): M
inline fun <V, M : MutableMap<in Float, in V>> FloatArray.associateWithTo(
    destination: M, 
    valueSelector: (Float) -> V
): M
inline fun <V, M : MutableMap<in Double, in V>> DoubleArray.associateWithTo(
    destination: M, 
    valueSelector: (Double) -> V
): M
inline fun <V, M : MutableMap<in Boolean, in V>> BooleanArray.associateWithTo(
    destination: M, 
    valueSelector: (Boolean) -> V
): M
inline fun <V, M : MutableMap<in Char, in V>> CharArray.associateWithTo(
    destination: M, 
    valueSelector: (Char) -> V
): M
@ExperimentalUnsignedTypes inline fun <V, M : MutableMap<in UInt, in V>> UIntArray.associateWithTo(
    destination: M, 
    valueSelector: (UInt) -> V
): M
@ExperimentalUnsignedTypes inline fun <V, M : MutableMap<in ULong, in V>> ULongArray.associateWithTo(
    destination: M, 
    valueSelector: (ULong) -> V
): M
@ExperimentalUnsignedTypes inline fun <V, M : MutableMap<in UByte, in V>> UByteArray.associateWithTo(
    destination: M, 
    valueSelector: (UByte) -> V
): M
@ExperimentalUnsignedTypes inline fun <V, M : MutableMap<in UShort, in V>> UShortArray.associateWithTo(
    destination: M, 
    valueSelector: (UShort) -> V
): M

使用給定數組的每個元素的鍵值對填充並返回 destination 可變映射,其中鍵是元素本身,值由應用於該鍵的 valueSelector 函數提供。

如果任何兩個元素相等,則最後一個元素將覆蓋映射中的前一個值。

例子:

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("Jacob", "Bernoulli"))
val withLengthOfNames = mutableMapOf<Person, Int>()
println("withLengthOfNames.isEmpty() is ${withLengthOfNames.isEmpty()}") // true

scientists.associateWithTo(withLengthOfNames) { it.firstName.length + it.lastName.length }

println("withLengthOfNames.isNotEmpty() is ${withLengthOfNames.isNotEmpty()}") // true
// Jacob Bernoulli only occurs once in the map because only the last pair with the same key gets added
println(withLengthOfNames) // {Grace Hopper=11, Jacob Bernoulli=14}
//sampleEnd
}

輸出:

withLengthOfNames.isEmpty() is true
withLengthOfNames.isNotEmpty() is true
{Grace Hopper=11, Jacob Bernoulli=14}

用法二

inline fun <K, V, M : MutableMap<in K, in V>> Iterable<K>.associateWithTo(
    destination: M, 
    valueSelector: (K) -> V
): M

使用給定集合的每個元素的鍵值對填充並返回 destination 可變映射,其中鍵是元素本身,值由應用於該鍵的 valueSelector 函數提供。

如果任何兩個元素相等,則最後一個元素將覆蓋映射中的前一個值。

例子:

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("Jacob", "Bernoulli"))
val withLengthOfNames = mutableMapOf<Person, Int>()
println("withLengthOfNames.isEmpty() is ${withLengthOfNames.isEmpty()}") // true

scientists.associateWithTo(withLengthOfNames) { it.firstName.length + it.lastName.length }

println("withLengthOfNames.isNotEmpty() is ${withLengthOfNames.isNotEmpty()}") // true
// Jacob Bernoulli only occurs once in the map because only the last pair with the same key gets added
println(withLengthOfNames) // {Grace Hopper=11, Jacob Bernoulli=14}
//sampleEnd
}

輸出:

withLengthOfNames.isEmpty() is true
withLengthOfNames.isNotEmpty() is true
{Grace Hopper=11, Jacob Bernoulli=14}