groupBy
所在位置是kotlin.collections.groupBy
,其相關用法介紹如下。
用法一
@ExperimentalUnsignedTypes inline fun <K> UIntArray.groupBy(
keySelector: (UInt) -> K
): Map<K, List<UInt>>
@ExperimentalUnsignedTypes inline fun <K> ULongArray.groupBy(
keySelector: (ULong) -> K
): Map<K, List<ULong>>
@ExperimentalUnsignedTypes inline fun <K> UByteArray.groupBy(
keySelector: (UByte) -> K
): Map<K, List<UByte>>
@ExperimentalUnsignedTypes inline fun <K> UShortArray.groupBy(
keySelector: (UShort) -> K
): Map<K, List<UShort>>
通過應用於每個元素的給定 keySelector 函數返回的鍵對原始數組的元素進行分組,並返回一個映射,其中每個組鍵與對應元素的列表相關聯。
返回的映射保留從原始數組生成的鍵的條目迭代順序。
例子:
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
用法二
inline fun <T, K, V> Array<out T>.groupBy(
keySelector: (T) -> K,
valueTransform: (T) -> V
): Map<K, List<V>>
inline fun <K, V> ByteArray.groupBy(
keySelector: (Byte) -> K,
valueTransform: (Byte) -> V
): Map<K, List<V>>
inline fun <K, V> ShortArray.groupBy(
keySelector: (Short) -> K,
valueTransform: (Short) -> V
): Map<K, List<V>>
inline fun <K, V> IntArray.groupBy(
keySelector: (Int) -> K,
valueTransform: (Int) -> V
): Map<K, List<V>>
inline fun <K, V> LongArray.groupBy(
keySelector: (Long) -> K,
valueTransform: (Long) -> V
): Map<K, List<V>>
inline fun <K, V> FloatArray.groupBy(
keySelector: (Float) -> K,
valueTransform: (Float) -> V
): Map<K, List<V>>
inline fun <K, V> DoubleArray.groupBy(
keySelector: (Double) -> K,
valueTransform: (Double) -> V
): Map<K, List<V>>
inline fun <K, V> BooleanArray.groupBy(
keySelector: (Boolean) -> K,
valueTransform: (Boolean) -> V
): Map<K, List<V>>
inline fun <K, V> CharArray.groupBy(
keySelector: (Char) -> K,
valueTransform: (Char) -> V
): Map<K, List<V>>
@ExperimentalUnsignedTypes inline fun <K, V> UIntArray.groupBy(
keySelector: (UInt) -> K,
valueTransform: (UInt) -> V
): Map<K, List<V>>
@ExperimentalUnsignedTypes inline fun <K, V> ULongArray.groupBy(
keySelector: (ULong) -> K,
valueTransform: (ULong) -> V
): Map<K, List<V>>
@ExperimentalUnsignedTypes inline fun <K, V> UByteArray.groupBy(
keySelector: (UByte) -> K,
valueTransform: (UByte) -> V
): Map<K, List<V>>
@ExperimentalUnsignedTypes inline fun <K, V> UShortArray.groupBy(
keySelector: (UShort) -> K,
valueTransform: (UShort) -> V
): Map<K, List<V>>
通過應用於元素的給定keySelector 函數返回的鍵對應用於原始數組的每個元素的valueTransform 函數返回的值進行分組,並返回一個映射,其中每個組鍵與對應值的列表相關聯。
返回的映射保留從原始數組生成的鍵的條目迭代順序。
例子:
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
用法三
通過應用於每個元素的給定 keySelector 函數返回的鍵對原始集合的元素進行分組,並返回一個映射,其中每個組鍵與對應元素的列表相關聯。
返回的映射保留從原始集合生成的鍵的條目迭代順序。
例子:
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
用法四
inline fun <T, K, V> Iterable<T>.groupBy(
keySelector: (T) -> K,
valueTransform: (T) -> V
): Map<K, List<V>>
通過應用於元素的給定keySelector 函數返回的鍵對應用於原始集合的每個元素的valueTransform 函數返回的值進行分組,並返回一個映射,其中每個組鍵與對應值的列表相關聯。
返回的映射保留從原始集合生成的鍵的條目迭代順序。
例子:
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
相關用法
- 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.groupBy。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。