1. Overview

In this tutorial, we’ll see a few idiomatic ways to generate a character frequency Map of Strings in Scala.

2. What Is a Frequency Map?

A character frequency map is a common name for a data structure holding information on how many times each character occurs in a given string or text. In Scala, it is usually represented as a Map. For the example string “hello”, here’s the corresponding frequency map:

scala> Map(e -> 1, h -> 1, l -> 2, o -> 1)

3. Generating a Frequency Map in Scala

Given some String, we can get a character frequency map in Scala. Many possible approaches exist, so we’ll learn some of the simplest.

3.1. Using groupBy() Method

To start, we’ll use the existing groupBy() function available in the standard lib together with the identity function:

scala> "hello".groupBy(identity)
res0: scala.collection.immutable.Map[Char,String] = Map(h -> h, e -> e, l -> ll, o -> o)

After performing this step, we can group each character. The returned Map contains entries where for each entry, the key is each distinct character in the string, and the value is a list of all the occurrences of that character. Finally, we count how many occurrences happen in the text:

scala> "hello".groupBy(identity).mapValues(_.length)
res1: scala.collection.immutable.Map[Char,Int] = Map(h -> 1, e -> 1, l -> 2, o -> 1)

And now, for each character, we can find how many times it appeared in the given text.

3.2. Using groupMapReduce() Method

Another solution is to use the groupMapReduce() method. This method has the same result as the previous example and can be seen as applying a groupBy() function followed by a transformation and reduction steps:

scala> "hello".groupMapReduce(identity)(_ => 1)(_ + _)
res2: scala.collection.immutable.Map[Char,Int] = Map(e -> 1, h -> 1, l -> 2, o -> 1) 

As we can see, it returns the same result as the previous example but uses a shorter syntax. In addition, the method receives functions as arguments: a group function, which is the same identity function we passed in the example of the previous section; a transformation function as the second argument, which in our case returns 1, acting as a counter; and finally the reduce function that will sum all the occurrences of each character.

4. Conclusion

In this article, we saw how to generate a frequency Map of Strings in Scala. Furthermore, we saw two different approaches using the standard library methods. One combines the String.groupBy() and Map.mapValues(), while the other uses the  String.groupMapReduce() method to compute our frequency map.

Comments are closed on this article!