0
0
Kotlinprogramming~10 mins

GroupBy for categorization in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - GroupBy for categorization
Start with List
Choose Key Function
Iterate over each item
Apply Key Function to item
Add item to group with that key
Repeat for all items
Return Map of groups
GroupBy takes a list and a function to decide keys, then puts items into groups by those keys.
Execution Sample
Kotlin
val fruits = listOf("apple", "banana", "apricot", "blueberry")
val grouped = fruits.groupBy { it.first() }
println(grouped)
Groups fruits by their first letter and prints the map of groups.
Execution Table
StepCurrent ItemKey Function ResultGroups Map StateAction
1"apple"'a'{ }Create new group 'a' with ["apple"]
2"banana"'b'{ 'a': ["apple"] }Create new group 'b' with ["banana"]
3"apricot"'a'{ 'a': ["apple"], 'b': ["banana"] }Add "apricot" to group 'a'
4"blueberry"'b'{ 'a': ["apple", "apricot"], 'b': ["banana"] }Add "blueberry" to group 'b'
5End-{ 'a': ["apple", "apricot"], 'b': ["banana", "blueberry"] }Return grouped map
💡 All items processed, grouping complete.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
groups{}{ 'a': ["apple"] }{ 'a': ["apple"], 'b': ["banana"] }{ 'a': ["apple", "apricot"], 'b': ["banana"] }{ 'a': ["apple", "apricot"], 'b': ["banana", "blueberry"] }{ 'a': ["apple", "apricot"], 'b': ["banana", "blueberry"] }
Key Moments - 2 Insights
Why does the group 'a' have two fruits after step 3?
Because at step 3, the key function returns 'a' for "apricot", so it adds "apricot" to the existing group 'a' which already has "apple" (see execution_table row 3).
What happens if the key function returns a key not seen before?
A new group is created for that key and the current item is added to it, as seen at step 2 when 'b' group is created for "banana".
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the groups map state after processing "banana"?
A{ 'a': ["apple", "apricot"] }
B{ 'b': ["banana"] }
C{ 'a': ["apple"], 'b': ["banana"] }
D{}
💡 Hint
Check execution_table row 2 under 'Groups Map State'.
At which step does the key function return 'b' for the first time?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Key Function Result' column in execution_table.
If we changed the key function to group by string length, what would be the key for "banana"?
A'b'
B6
C'banana'
D1
💡 Hint
String length of "banana" is the number of characters, check variable_tracker for similar logic.
Concept Snapshot
GroupBy usage in Kotlin:
- Takes a list and a key selector function.
- Iterates each item, applies key function.
- Groups items by keys in a Map.
- Returns Map<Key, List<Item>>.
- Useful for categorizing data simply.
Full Transcript
This example shows how Kotlin's groupBy function works. We start with a list of fruits. The groupBy function uses a key function that picks the first letter of each fruit. As the program runs, it looks at each fruit, finds its first letter, and adds the fruit to the group for that letter. For example, "apple" goes to group 'a'. When "apricot" comes, it also goes to 'a' group. "banana" and "blueberry" go to 'b' group. At the end, we have a map with keys 'a' and 'b', each holding a list of fruits starting with those letters. This helps organize data by categories easily.