The 'groupBy' function in Kotlin is a powerful tool for organizing collections into a map, where the keys are derived from a specified selector function. This is particularly useful when you want to categorize elements based on a common property. For example, you can group a list of people by their age or occupation, making it easier to analyze or display the data.
val people = listOf(
Person("Alice", 30),
Person("Bob", 25),
Person("Charlie", 30),
Person("David", 25)
)
val groupedByAge = people.groupBy { it.age }
groupedByAge.forEach { (age, persons) ->
println("Age: $age -> ${persons.map { it.name }}")
}
data class Person(val name: String, val age: Int)