groupByTo
所在位置是kotlin.collections.groupByTo
,其相關用法介紹如下。
用法一
inline fun <T, K, M : MutableMap<in K, MutableList<T>>> Array<out T>.groupByTo(
destination: M,
keySelector: (T) -> K
): M
inline fun <K, M : MutableMap<in K, MutableList<Byte>>> ByteArray.groupByTo(
destination: M,
keySelector: (Byte) -> K
): M
inline fun <K, M : MutableMap<in K, MutableList<Short>>> ShortArray.groupByTo(
destination: M,
keySelector: (Short) -> K
): M
inline fun <K, M : MutableMap<in K, MutableList<Int>>> IntArray.groupByTo(
destination: M,
keySelector: (Int) -> K
): M
inline fun <K, M : MutableMap<in K, MutableList<Long>>> LongArray.groupByTo(
destination: M,
keySelector: (Long) -> K
): M
inline fun <K, M : MutableMap<in K, MutableList<Float>>> FloatArray.groupByTo(
destination: M,
keySelector: (Float) -> K
): M
inline fun <K, M : MutableMap<in K, MutableList<Double>>> DoubleArray.groupByTo(
destination: M,
keySelector: (Double) -> K
): M
inline fun <K, M : MutableMap<in K, MutableList<Boolean>>> BooleanArray.groupByTo(
destination: M,
keySelector: (Boolean) -> K
): M
inline fun <K, M : MutableMap<in K, MutableList<Char>>> CharArray.groupByTo(
destination: M,
keySelector: (Char) -> K
): M
@ExperimentalUnsignedTypes inline fun <K, M : MutableMap<in K, MutableList<UInt>>> UIntArray.groupByTo(
destination: M,
keySelector: (UInt) -> K
): M
@ExperimentalUnsignedTypes inline fun <K, M : MutableMap<in K, MutableList<ULong>>> ULongArray.groupByTo(
destination: M,
keySelector: (ULong) -> K
): M
@ExperimentalUnsignedTypes inline fun <K, M : MutableMap<in K, MutableList<UByte>>> UByteArray.groupByTo(
destination: M,
keySelector: (UByte) -> K
): M
@ExperimentalUnsignedTypes inline fun <K, M : MutableMap<in K, MutableList<UShort>>> UShortArray.groupByTo(
destination: M,
keySelector: (UShort) -> K
): M
通過應用於每個元素的給定keySelector 函數返回的鍵對原始數組的元素進行分組,並將與對應元素列表關聯的每個組鍵放入destination 映射。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val words = listOf("a", "abc", "ab", "def", "abcd")
val byLength = words.groupBy { it.length }
println(byLength.keys) // [1, 3, 2, 4]
println(byLength.values) // [[a], [abc, def], [ab], [abcd]]
val mutableByLength: MutableMap<Int, MutableList<String>> = words.groupByTo(mutableMapOf()) { it.length }
// same content as in byLength map, but the map is mutable
println("mutableByLength == byLength is ${mutableByLength == byLength}") // true
//sampleEnd
}
輸出:
[1, 3, 2, 4] [[a], [abc, def], [ab], [abcd]] mutableByLength == byLength is true
返回destination Map。
用法二
inline fun <T, K, V, M : MutableMap<in K, MutableList<V>>> Array<out T>.groupByTo(
destination: M,
keySelector: (T) -> K,
valueTransform: (T) -> V
): M
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> ByteArray.groupByTo(
destination: M,
keySelector: (Byte) -> K,
valueTransform: (Byte) -> V
): M
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> ShortArray.groupByTo(
destination: M,
keySelector: (Short) -> K,
valueTransform: (Short) -> V
): M
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> IntArray.groupByTo(
destination: M,
keySelector: (Int) -> K,
valueTransform: (Int) -> V
): M
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> LongArray.groupByTo(
destination: M,
keySelector: (Long) -> K,
valueTransform: (Long) -> V
): M
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> FloatArray.groupByTo(
destination: M,
keySelector: (Float) -> K,
valueTransform: (Float) -> V
): M
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> DoubleArray.groupByTo(
destination: M,
keySelector: (Double) -> K,
valueTransform: (Double) -> V
): M
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> BooleanArray.groupByTo(
destination: M,
keySelector: (Boolean) -> K,
valueTransform: (Boolean) -> V
): M
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> CharArray.groupByTo(
destination: M,
keySelector: (Char) -> K,
valueTransform: (Char) -> V
): M
@ExperimentalUnsignedTypes inline fun <K, V, M : MutableMap<in K, MutableList<V>>> UIntArray.groupByTo(
destination: M,
keySelector: (UInt) -> K,
valueTransform: (UInt) -> V
): M
@ExperimentalUnsignedTypes inline fun <K, V, M : MutableMap<in K, MutableList<V>>> ULongArray.groupByTo(
destination: M,
keySelector: (ULong) -> K,
valueTransform: (ULong) -> V
): M
@ExperimentalUnsignedTypes inline fun <K, V, M : MutableMap<in K, MutableList<V>>> UByteArray.groupByTo(
destination: M,
keySelector: (UByte) -> K,
valueTransform: (UByte) -> V
): M
@ExperimentalUnsignedTypes inline fun <K, V, M : MutableMap<in K, MutableList<V>>> UShortArray.groupByTo(
destination: M,
keySelector: (UShort) -> K,
valueTransform: (UShort) -> V
): M
將valueTransform函數返回的值分組到原始數組的每個元素上,由給定的keySelector函數返回的鍵應用於元素並將每個組鍵映射到destination映射與對應值列表相關聯的每個組鍵.
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val nameToTeam = listOf("Alice" to "Marketing", "Bob" to "Sales", "Carol" to "Marketing")
val namesByTeam = nameToTeam.groupBy({ it.second }, { it.first })
println(namesByTeam) // {Marketing=[Alice, Carol], Sales=[Bob]}
val mutableNamesByTeam = nameToTeam.groupByTo(HashMap(), { it.second }, { it.first })
// same content as in namesByTeam map, but the map is mutable
println("mutableNamesByTeam == namesByTeam is ${mutableNamesByTeam == namesByTeam}") // true
//sampleEnd
}
輸出:
{Marketing=[Alice, Carol], Sales=[Bob]} mutableNamesByTeam == namesByTeam is true
返回destination Map。
用法三
inline fun <T, K, M : MutableMap<in K, MutableList<T>>> Iterable<T>.groupByTo(
destination: M,
keySelector: (T) -> K
): M
通過應用於每個元素的給定keySelector 函數返回的鍵對原始集合的元素進行分組,並將與對應元素列表關聯的每個組鍵放入destination 映射。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val words = listOf("a", "abc", "ab", "def", "abcd")
val byLength = words.groupBy { it.length }
println(byLength.keys) // [1, 3, 2, 4]
println(byLength.values) // [[a], [abc, def], [ab], [abcd]]
val mutableByLength: MutableMap<Int, MutableList<String>> = words.groupByTo(mutableMapOf()) { it.length }
// same content as in byLength map, but the map is mutable
println("mutableByLength == byLength is ${mutableByLength == byLength}") // true
//sampleEnd
}
輸出:
[1, 3, 2, 4] [[a], [abc, def], [ab], [abcd]] mutableByLength == byLength is true
返回destination Map。
用法四
inline fun <T, K, V, M : MutableMap<in K, MutableList<V>>> Iterable<T>.groupByTo(
destination: M,
keySelector: (T) -> K,
valueTransform: (T) -> V
): M
由應用於原始集合的每個元素的valueTransform函數返回的值由應用於元素的給定keySelector函數返回的鍵分組,並將每個組鍵映射到與對應值列表關聯的destination .
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val nameToTeam = listOf("Alice" to "Marketing", "Bob" to "Sales", "Carol" to "Marketing")
val namesByTeam = nameToTeam.groupBy({ it.second }, { it.first })
println(namesByTeam) // {Marketing=[Alice, Carol], Sales=[Bob]}
val mutableNamesByTeam = nameToTeam.groupByTo(HashMap(), { it.second }, { it.first })
// same content as in namesByTeam map, but the map is mutable
println("mutableNamesByTeam == namesByTeam is ${mutableNamesByTeam == namesByTeam}") // true
//sampleEnd
}
輸出:
{Marketing=[Alice, Carol], Sales=[Bob]} mutableNamesByTeam == namesByTeam is true
返回destination Map。
相關用法
- Kotlin groupByTo用法及代碼示例
- Kotlin groupBy用法及代碼示例
- Kotlin groupingBy用法及代碼示例
- Kotlin getValue用法及代碼示例
- Kotlin getOrPut用法及代碼示例
- Kotlin getOrElse用法及代碼示例
- Kotlin getOrNull用法及代碼示例
- Kotlin associateBy用法及代碼示例
- Kotlin all用法及代碼示例
- Kotlin map用法及代碼示例
- Kotlin filterNot用法及代碼示例
- Kotlin reduceRight用法及代碼示例
- Kotlin Random.Default用法及代碼示例
- Kotlin Byte.inc用法及代碼示例
- Kotlin Double.dec用法及代碼示例
- Kotlin windowedSequence用法及代碼示例
- Kotlin contentToString用法及代碼示例
- Kotlin commonPrefixWith用法及代碼示例
- Kotlin MatchResult.Destructured用法及代碼示例
- Kotlin Delegates.notNull用法及代碼示例
- Kotlin ifBlank用法及代碼示例
- Kotlin filterNotTo用法及代碼示例
- Kotlin Triple.<init>用法及代碼示例
- Kotlin Duration.toString用法及代碼示例
- Kotlin windowed用法及代碼示例
注:本文由純淨天空篩選整理自kotlinlang.org大神的英文原創作品 kotlin.collections.groupByTo。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。