// Created on savesnippets.com · https://savesnippets.com/jcOKHCdYQIpNhK data class User(val name: String, val team: String, val active: Boolean) fun main() { val users = listOf( User("Alice", "A", true), User("Bob", "B", false), User("Cara", "A", true), User("Dave", "B", true), ) // groupBy — bucket by any key val byTeam: Map> = users.groupBy { it.team } println(byTeam.mapValues { (_, v) -> v.size }) // {A=2, B=2} // groupingBy + eachCount — count per group in one pass val countByTeam = users.groupingBy { it.team }.eachCount() println(countByTeam) // {A=2, B=2} // partition — split by a boolean predicate val (active, inactive) = users.partition { it.active } println("active: ${active.size}, inactive: ${inactive.size}") // associateBy — like groupBy but assumes UNIQUE keys → Map val byName = users.associateBy { it.name } println(byName["Alice"]) }