associateTo
所在位置是kotlin.collections.associateTo
,其相關用法介紹如下。
用法一
inline fun <T, K, V, M : MutableMap<in K, in V>> Array<out T>.associateTo(
destination: M,
transform: (T) -> Pair<K, V>
): M
inline fun <K, V, M : MutableMap<in K, in V>> ByteArray.associateTo(
destination: M,
transform: (Byte) -> Pair<K, V>
): M
inline fun <K, V, M : MutableMap<in K, in V>> ShortArray.associateTo(
destination: M,
transform: (Short) -> Pair<K, V>
): M
inline fun <K, V, M : MutableMap<in K, in V>> IntArray.associateTo(
destination: M,
transform: (Int) -> Pair<K, V>
): M
inline fun <K, V, M : MutableMap<in K, in V>> LongArray.associateTo(
destination: M,
transform: (Long) -> Pair<K, V>
): M
inline fun <K, V, M : MutableMap<in K, in V>> FloatArray.associateTo(
destination: M,
transform: (Float) -> Pair<K, V>
): M
inline fun <K, V, M : MutableMap<in K, in V>> DoubleArray.associateTo(
destination: M,
transform: (Double) -> Pair<K, V>
): M
inline fun <K, V, M : MutableMap<in K, in V>> BooleanArray.associateTo(
destination: M,
transform: (Boolean) -> Pair<K, V>
): M
inline fun <K, V, M : MutableMap<in K, in V>> CharArray.associateTo(
destination: M,
transform: (Char) -> Pair<K, V>
): M
填充並返回 destination 可變映射,其中鍵值對由 transform 函數提供,應用於給定數組的每個元素。
如果兩對中的任何一對具有相同的 key ,則最後一個將添加到Map中。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val charCodes = intArrayOf(72, 69, 76, 76, 79)
val byChar = mutableMapOf<Int, Char>()
charCodes.associateTo(byChar) { it to Char(it) }
// 76=L only occurs once because only the last pair with the same key gets added
println(byChar) // {72=H, 69=E, 76=L, 79=O}
//sampleEnd
}
輸出:
{72=H, 69=E, 76=L, 79=O}
用法二
inline fun <T, K, V, M : MutableMap<in K, in V>> Iterable<T>.associateTo(
destination: M,
transform: (T) -> Pair<K, V>
): M
填充並返回 destination 可變映射,其中鍵值對由 transform 函數提供,應用於給定集合的每個元素。
如果兩對中的任何一對具有相同的 key ,則最後一個將添加到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.associateTo(byLastName) { it.lastName to 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 associateTo用法及代碼示例
- Kotlin associateBy用法及代碼示例
- Kotlin associateWithTo用法及代碼示例
- Kotlin associate用法及代碼示例
- Kotlin associateWith用法及代碼示例
- 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.collections.associateTo。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。