Scala Tutorial - Learn How To Use GroupBy Function With Examples

By Nadim Bahadoor | Last updated: March 16, 2018 at 14:33 pm

Overview

In this tutorial, we will learn how to use the groupBy function with examples on collection data structures in Scala. The groupBy function is applicable to both Scala's Mutable and Immutable collection data structures.

 

The groupBy method takes a predicate function as its parameter and uses it to group elements by key and values into a Map collection.

 

As per the Scala documentation, the definition of the groupBy method is as follows:


groupBy[K](f: (A) ⇒ K): immutable.Map[K, Repr]

 

The groupBy method is a member of the TraversableLike trait.


Steps

1. How to initialize a Sequence of donuts

The code below shows how to initialize a sequence of donut elements where each element in the sequence is of type String.


println("Step 1: How to initialize a Sequence of donuts")
val donuts: Seq[String] = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut")
println(s"Elements of donuts = $donuts")

 

You should see the following output when you run your Scala application in IntelliJ:


Step 1: How to initialize a Sequence of donuts
Elements of donuts = List(Plain Donut, Strawberry Donut, Glazed Donut)

 

2. How to group elements in a sequence using the groupBy function

The code below shows how to use the groupBy method to group elements in the donut sequence by the first character for each donut.


println("\nStep 2: How to group elements in a sequence using the groupBy function")
val donutsGroup: Map[Char, Seq[String]] = donuts.groupBy(_.charAt(0))
println(s"Group elements in the donut sequence by the first letter of the donut name = $donutsGroup")

You should see the following output when you run your Scala application in IntelliJ:


Step 2: How to group elements in a sequence using the groupBy function
Group elements in the donut sequence by the first letter of the donut name = Map(S -> List(Strawberry Donut), G -> List(Glazed Donut), P -> List(Plain Donut))

NOTE:

  • We end up with a Map where the letter S points to Strawberry Donut, letter G points to Glazed Donut and P points to Plain Donut.

3. How to create a case class to represent Donut objects

The code below shows how to use a Case Class to represent donut objects.


println("\nStep 3: How to create a case class to represent Donut objects")
case class Donut(name: String, price: Double)

4. How to create a Sequence of type Donut

The code below shows how to use the Donut case class from Step 3 and create a sequence of donut elements of type Donut.


println("\nStep 4: How to create a Sequence of type Donut")
val donuts2: Seq[Donut] = Seq(Donut("Plain Donut", 1.5), Donut("Strawberry Donut", 2.0), Donut("Glazed Donut", 2.5))
println(s"Elements of donuts2 = $donuts2")

You should see the following output when you run your Scala application in IntelliJ:


Step 4: How to create a Sequence of type Donut
Elements of donuts2 = List(Donut(Plain Donut,1.5), Donut(Strawberry Donut,2.0), Donut(Glazed Donut,2.5))

 

5. How to group case classes donut objects by the name property

The code below shows how to group the donut objects represented by case class Donut from Step 3 by their name property using the groupBy method.


println(s"\nStep 5: How to group case classes donut objects by the name property")
val donutsGroup2: Map[String, Seq[Donut]] = donuts2.groupBy(_.name)
println(s"Group element in the sequence of type Donut grouped by the donut name = $donutsGroup2")

You should see the following output when you run your Scala application in IntelliJ:


Step 5: How to group case classes donut objects by the name property
Group element in the sequence of type Donut grouped by the donut name = Map(Glazed Donut -> List(Donut(Glazed Donut,2.5)), Plain Donut -> List(Donut(Plain Donut,1.5)), Strawberry Donut -> List(Donut(Strawberry Donut,2.0)))




 

This concludes our tutorial on Learn How To Use GroupBy Function With Examples and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

Summary

In this tutorial, we went over the following:

  • How to initialize a Sequence of donuts
  • How to group elements in a sequence using the groupBy function
  • How to create a case class to represent Donut objects
  • How to create a Sequence of type Donut
  • How to group case classes donut objects by the name property

Tip

  • Review the tutorials on Mutable and Immutable collection data structures in Scala.

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will show you how to use head function.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Technology and Finance Consultant with over 14 years of hands-on experience building large scale systems in the Financial (Electronic Trading Platforms), Risk, Insurance and Life Science sectors. I am self-driven and passionate about Finance, Distributed Systems, Functional Programming, Big Data, Semantic Data (Graph) and Machine Learning.
Other allaboutscala.com tutorials you may like: