Skip to content

Commit 54224bc

Browse files
authored
update: adding iteration example for groups
* update: adding example on iterating through groups * implementing review comments * implementing Ilya's comments * implementing comments from Ale
1 parent 2645856 commit 54224bc

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

docs/topics/collection-grouping.md

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,39 @@
22

33
The Kotlin standard library provides extension functions for grouping collection elements.
44
The basic function [`groupBy()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/group-by.html) takes a
5-
lambda function and returns a `Map`. In this map, each key is the lambda result and the corresponding value is the `List`
5+
lambda function and returns a `Map`. In this map, each key is the lambda result, and the corresponding value is the `List`
66
of elements on which this result is returned. This function can be used, for example, to group a list of `String`s by
77
their first letter.
88

99
You can also call `groupBy()` with a second lambda argument – a value transformation function.
1010
In the result map of `groupBy()` with two lambdas, the keys produced by `keySelector` function are mapped to the results
1111
of the value transformation function instead of the original elements.
1212

13-
```kotlin
13+
This example illustrates using the `groupBy()` function to group the strings by their first letter, iterating through
14+
the groups on the resulting `Map` with the `for` operator, and then transforming the values to uppercase using the `keySelector` function:
1415

16+
```kotlin
1517
fun main() {
1618
//sampleStart
1719
val numbers = listOf("one", "two", "three", "four", "five")
1820

19-
println(numbers.groupBy { it.first().uppercase() })
20-
println(numbers.groupBy(keySelector = { it.first() }, valueTransform = { it.uppercase() }))
21+
// Groups the strings by their first letter using groupBy()
22+
val groupedByFirstLetter = numbers.groupBy { it.first().uppercase() }
23+
println(groupedByFirstLetter)
24+
// {O=[one], T=[two, three], F=[four, five]}
25+
26+
// Iterates through each group and prints the key and its associated values
27+
for ((key, value) in groupedByFirstLetter) {
28+
println("Key: $key, Values: $value")
29+
}
30+
// Key: O, Values: [one]
31+
// Key: T, Values: [two, three]
32+
// Key: F, Values: [four, five]
33+
34+
// Groups the strings by their first letter and transforms the values to uppercase
35+
val groupedAndTransformed = numbers.groupBy(keySelector = { it.first() }, valueTransform = { it.uppercase() })
36+
println(groupedAndTransformed)
37+
// {o=[ONE], t=[TWO, THREE], f=[FOUR, FIVE]}
2138
//sampleEnd
2239
}
2340
```
@@ -30,21 +47,36 @@ right before the operation execution.
3047

3148
Namely, `Grouping` supports the following operations:
3249

33-
* [`eachCount()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/each-count.html) counts the elements in each group.
50+
* [`eachCount()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/each-count.html) counts the elements in each group.
3451
* [`fold()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/fold.html) and [`reduce()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/reduce.html)
35-
perform [fold and reduce](collection-aggregate.md#fold-and-reduce) operations on each group as a separate collection
36-
and return the results.
52+
perform [fold and reduce](collection-aggregate.md#fold-and-reduce) operations on each group as a separate collection
53+
and return the results.
3754
* [`aggregate()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/aggregate.html) applies a given operation
38-
subsequently to all the elements in each group and returns the result.
39-
This is the generic way to perform any operations on a `Grouping`. Use it to implement custom operations when fold or reduce are not enough.
55+
subsequently to all the elements in each group and returns the result.
56+
This is the generic way to perform any operations on a `Grouping`. Use it to implement custom operations when fold or reduce are not enough.
4057

41-
```kotlin
58+
You can use the `for` operator on the resulting `Map` to iterate through the groups created by the `groupingBy()` function.
59+
This allows you to access each key and the count of elements associated with that key.
60+
61+
The following example demonstrates how to group strings by their first letter using the `groupingBy()` function,
62+
count the elements in each group, and then iterate through each group to print the key and count of elements:
4263

64+
```kotlin
4365
fun main() {
4466
//sampleStart
45-
val numbers = listOf("one", "two", "three", "four", "five", "six")
46-
println(numbers.groupingBy { it.first() }.eachCount())
67+
val numbers = listOf("one", "two", "three", "four", "five")
68+
69+
// Groups the strings by their first letter using groupingBy() and counts the elements in each group
70+
val grouped = numbers.groupingBy { it.first() }.eachCount()
71+
72+
// Iterates through each group and prints the key and its associated values
73+
for ((key, count) in grouped) {
74+
println("Key: $key, Count: $count")
75+
// Key: o, Count: 1
76+
// Key: t, Count: 2
77+
// Key: f, Count: 2
78+
}
4779
//sampleEnd
4880
}
4981
```
50-
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}
82+
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}

0 commit comments

Comments
 (0)