Using the 'groupBy' Function for Organizing Collections

The 'groupBy' function in Kotlin is a powerful tool for organizing a collection into a map of lists, where each key in the map corresponds to a distinct value obtained by applying a specified selector function. This is particularly useful for categorizing items based on certain attributes, making it easier to manage and analyze grouped data.

val items = listOf("apple", "banana", "avocado", "blueberry", "cherry")

// Grouping items by their first letter
val groupedByFirstLetter = items.groupBy { it.first() }

println(groupedByFirstLetter)
// Output: {a=[apple, avocado], b=[banana, blueberry], c=[cherry]}