Scala Tutorial - Learn How To Use FlatMap Function

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

Overview

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

 

The flatMap method takes a predicate function, applies it to every element in the collection. It then returns a new collection by using the elements returned by the predicate function. The flatMap method is essentially a combination of the map method being run first followed by the flatten method.

 

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


def flatMap[B](f: (A) ⇒ GenTraversableOnce[B]): TraversableOnce[B]

 

The flatMap method is a member of the TraversableLike trait but there are specialized versions of the flatMap methods for given collection types.

Steps

1. How to initialize a Sequence of donuts

The code below shows how to initialize a Sequence of Donut elements of type String.


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

 

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 donuts1 = List(Plain Donut, Strawberry Donut, Glazed Donut)

 

2. How to initialize another Sequence of donuts

The code below shows how to initialize another Sequence of Donut elements of type String .


println("\nStep 2: How to initialize another Sequence of donuts")
val donuts2: Seq[String] = Seq("Vanilla Donut", "Glazed Donut")
println(s"Elements of donuts2 = $donuts2")

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


Step 2: How to initialize another Sequence of donuts
Elements of donuts2 = List(Vanilla Donut, Glazed Donut)

 

3. How to create a List of donuts initialized using the two Sequences from Step 1 and Step 2

The code below shows how to create a List of donut elements and initialize the List by using the two donut sequences from Step 1 and Step 2 respectively.


println("\nStep 3: How to create a List of donuts initialized using the two Sequences from Step 1 and Step 2")
val listDonuts: List[Seq[String]] = List(donuts1, donuts2)
println(s"Elements of listDonuts = $listDonuts")

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

Step 3: How to create a List of donuts initialized using the two Sequences from Step 1 and Step 2
Elements of listDonuts = List(List(Plain Donut, Strawberry Donut, Glazed Donut), List(Vanilla Donut, Glazed Donut))

NOTE:

  • We end up with a List containing two inner lists: List(List(Plain Donut, Strawberry Donut, Glazed Donut), List(Vanilla Donut, Glazed Donut))
  • What if you need to produce a single List of donut elements as opposed to having a List with two inner lists? We can achieve this using the flatMap method as shown in Step 4 below.

4. How to return a single list of donut using the flatMap function

The code below shows how to use the flatMap method on the List of donuts which contains two inner donut lists and return a single donut List of type String.



println("\nStep 4: How to return a single list of donut using the flatMap function")
val listDonutsFromFlatMap: List[String] = listDonuts.flatMap(seq => seq)
println(s"Elements of listDonutsFromFlatMap as a flatMap as a single list = $listDonutsFromFlatMap")

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


Step 4: How to return a single list of donut using the flatMap function
Elements of listDonutsFromFlatMap as a flatMap as a single list = List(Plain Donut, Strawberry Donut, Glazed Donut, Vanilla Donut, Glazed Donut)

 

This concludes our tutorial on Learn How To Use FlatMap Function 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 initialize another Sequence of donuts
  • How to create a List of donuts initialized using the two Sequences from Step 1 and Step 2
  • How to return a single list of donut using the flatMap function

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 flatten functions.

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: