Add groupingBy and eachCount sample.
This commit is contained in:
@@ -59,6 +59,18 @@ class Collections {
|
||||
// same content as in namesByTeam map, but the map is mutable
|
||||
assertTrue(mutableNamesByTeam == namesByTeam)
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun groupingByEachCount() {
|
||||
val words = "one two three four five six seven eight nine ten".split(' ')
|
||||
val frequenciesByFirstChar = words.groupingBy { it.first() }.eachCount()
|
||||
println("Counting first letters:")
|
||||
assertPrints(frequenciesByFirstChar, "{o=1, t=3, f=2, s=2, e=1, n=1}")
|
||||
|
||||
val moreWords = "eleven twelve".split(' ')
|
||||
val moreFrequencies = moreWords.groupingBy { it.first() }.eachCountTo(frequenciesByFirstChar.toMutableMap())
|
||||
assertPrints(moreFrequencies, "{o=1, t=4, f=2, s=2, e=2, n=1}")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user