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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。