Groups elements of the original array by the key returned by the given keySelector function applied to each element and puts to the destination map each group key associated with a list of corresponding elements.
Since Kotlin
1.0Return
The destination map.
Samples
import kotlin.test.*
fun main() {
val words = listOf("a", "abc", "ab", "def", "abcd")
val byLength = words.groupBy { it.length }
println(byLength.keys)
println(byLength.values)
val mutableByLength: MutableMap<Int, MutableList<String>> = words.groupByTo(mutableMapOf()) { it.length }
println("mutableByLength == byLength is ${mutableByLength == byLength}")
}
Target: JVMRunning on v.2.1.20
Groups values returned by the valueTransform function applied to each element of the original array by the key returned by the given keySelector function applied to the element and puts to the destination map each group key associated with a list of corresponding values.
Since Kotlin
1.0Return
The destination map.
Samples
import kotlin.test.*
fun main() {
val nameToTeam = listOf("Alice" to "Marketing", "Bob" to "Sales", "Carol" to "Marketing")
val namesByTeam = nameToTeam.groupBy({ it.second }, { it.first })
println(namesByTeam)
val mutableNamesByTeam = nameToTeam.groupByTo(HashMap(), { it.second }, { it.first })
println("mutableNamesByTeam == namesByTeam is ${mutableNamesByTeam == namesByTeam}")
}
Target: JVMRunning on v.2.1.20
Groups elements of the original collection by the key returned by the given keySelector function applied to each element and puts to the destination map each group key associated with a list of corresponding elements.
Since Kotlin
1.0Return
The destination map.
Samples
import kotlin.test.*
fun main() {
val words = listOf("a", "abc", "ab", "def", "abcd")
val byLength = words.groupBy { it.length }
println(byLength.keys)
println(byLength.values)
val mutableByLength: MutableMap<Int, MutableList<String>> = words.groupByTo(mutableMapOf()) { it.length }
println("mutableByLength == byLength is ${mutableByLength == byLength}")
}
Target: JVMRunning on v.2.1.20
Groups values returned by the valueTransform function applied to each element of the original collection by the key returned by the given keySelector function applied to the element and puts to the destination map each group key associated with a list of corresponding values.
Since Kotlin
1.0Return
The destination map.
Samples
import kotlin.test.*
fun main() {
val nameToTeam = listOf("Alice" to "Marketing", "Bob" to "Sales", "Carol" to "Marketing")
val namesByTeam = nameToTeam.groupBy({ it.second }, { it.first })
println(namesByTeam)
val mutableNamesByTeam = nameToTeam.groupByTo(HashMap(), { it.second }, { it.first })
println("mutableNamesByTeam == namesByTeam is ${mutableNamesByTeam == namesByTeam}")
}
Target: JVMRunning on v.2.1.20
Groups elements of the original array by the key returned by the given keySelector function applied to each element and puts to the destination map each group key associated with a list of corresponding elements.
Since Kotlin
1.3Return
The destination map.
Samples
import kotlin.test.*
fun main() {
val words = listOf("a", "abc", "ab", "def", "abcd")
val byLength = words.groupBy { it.length }
println(byLength.keys)
println(byLength.values)
val mutableByLength: MutableMap<Int, MutableList<String>> = words.groupByTo(mutableMapOf()) { it.length }
println("mutableByLength == byLength is ${mutableByLength == byLength}")
}
Target: JVMRunning on v.2.1.20
Groups values returned by the valueTransform function applied to each element of the original array by the key returned by the given keySelector function applied to the element and puts to the destination map each group key associated with a list of corresponding values.
Since Kotlin
1.3Return
The destination map.
Samples
import kotlin.test.*
fun main() {
val nameToTeam = listOf("Alice" to "Marketing", "Bob" to "Sales", "Carol" to "Marketing")
val namesByTeam = nameToTeam.groupBy({ it.second }, { it.first })
println(namesByTeam)
val mutableNamesByTeam = nameToTeam.groupByTo(HashMap(), { it.second }, { it.first })
println("mutableNamesByTeam == namesByTeam is ${mutableNamesByTeam == namesByTeam}")
}
Target: JVMRunning on v.2.1.20