Add groupingBy and eachCount sample.

This commit is contained in:
Ilya Gorbunov
2017-02-28 22:01:27 +03:00
parent 75ae42121b
commit a04e6de047
15 changed files with 42 additions and 0 deletions
@@ -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}")
}
}
}