associateBy
所在位置是kotlin.collections.associateBy
,其相關用法介紹如下。
用法一
返回一個 Map,其中包含給定數組中的元素,該數組由應用於每個元素的 keySelector 函數返回的鍵索引。
如果任意兩個元素具有由keySelector 返回的相同鍵,則將最後一個元素添加到Map中。
返回的映射保留原始數組的條目迭代順序。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val charCodes = intArrayOf(72, 69, 76, 76, 79)
val byChar = charCodes.associateBy { Char(it) }
// 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
}
輸出:
{H=72, E=69, L=76, O=79}
用法二
inline fun <T, K, V> Array<out T>.associateBy(
keySelector: (T) -> K,
valueTransform: (T) -> V
): Map<K, V>
inline fun <K, V> ByteArray.associateBy(
keySelector: (Byte) -> K,
valueTransform: (Byte) -> V
): Map<K, V>
inline fun <K, V> ShortArray.associateBy(
keySelector: (Short) -> K,
valueTransform: (Short) -> V
): Map<K, V>
inline fun <K, V> IntArray.associateBy(
keySelector: (Int) -> K,
valueTransform: (Int) -> V
): Map<K, V>
inline fun <K, V> LongArray.associateBy(
keySelector: (Long) -> K,
valueTransform: (Long) -> V
): Map<K, V>
inline fun <K, V> FloatArray.associateBy(
keySelector: (Float) -> K,
valueTransform: (Float) -> V
): Map<K, V>
inline fun <K, V> DoubleArray.associateBy(
keySelector: (Double) -> K,
valueTransform: (Double) -> V
): Map<K, V>
inline fun <K, V> BooleanArray.associateBy(
keySelector: (Boolean) -> K,
valueTransform: (Boolean) -> V
): Map<K, V>
inline fun <K, V> CharArray.associateBy(
keySelector: (Char) -> K,
valueTransform: (Char) -> V
): Map<K, V>
返回一個 Map,其中包含由 valueTransform 提供的值並由應用於給定數組元素的 keySelector 函數索引。
如果任意兩個元素具有由keySelector 返回的相同鍵,則將最後一個元素添加到Map中。
返回的映射保留原始數組的條目迭代順序。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val charCodes = intArrayOf(65, 65, 66, 67, 68, 69)
val byUpperCase = charCodes.associateBy({ 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}
用法三
返回一個Map,其中包含給定集合中的元素,這些元素由應用於每個元素的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 = scientists.associateBy { it.lastName }
// 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
}
輸出:
{Hopper=Grace Hopper, Bernoulli=Johann Bernoulli}
用法四
inline fun <T, K, V> Iterable<T>.associateBy(
keySelector: (T) -> K,
valueTransform: (T) -> V
): Map<K, V>
返回一個 Map,其中包含由 valueTransform 提供的值,並由應用於給定集合元素的 keySelector 函數索引。
如果任意兩個元素具有由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 = scientists.associateBy({ it.lastName }, { it.firstName })
// 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
}
輸出:
{Hopper=Grace, Bernoulli=Johann}
相關用法
- Kotlin associateBy用法及代碼示例
- Kotlin associateByTo用法及代碼示例
- 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.collections.associateBy。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。