Scala Tutorial - Learn How To Use Scala's Mutable Map

By Nadim Bahadoor | Last updated: July 25, 2017 at 14:45 pm

Overview

In this tutorial, we will learn how to use Scala's Mutable Map to perform common operations such as initialize a Map, access elements by key, add and remove elements and create an empty Map.

 

More advanced functions such as aggregate, fold, reduce, map, flatMap etc on the Mutable Map will be discussed in Chapter 8 on Collection Functions.

 

What is a Map?

As per the Scala Documentation, a Map is a collection of key and value pairs which are stored internally using a Hash Table data structure.

 

Steps

1. How to initialize a Map with 3 elements

The code below shows how to initialize a Map with 3 elements.


import scala.collection.mutable.Map
println("\nStep 1: How to initialize a Map with 3 elements")
val map1: Map[String, String] = Map(("PD","Plain Donut"),("SD","Strawberry Donut"),("CD","Chocolate Donut"))
println(s"Elements of map1 = $map1")

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


Step 1: How to initialize a Map with 3 elements
Elements of map1 = Map(CD -> Chocolate Donut, SD -> Strawberry Donut, PD -> Plain Donut)

 

2. How to initialize a Map using key -> value notation

The code below shows how to initialize a Map using key -> value notation.


println("\nStep 2: How to initialize a Map using key -> value notation")
val map2: Map[String, String] = Map("VD"-> "Vanilla Donut", "GD" -> "Glazed Donut")
println(s"Elements of map2 = $map2")

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

Step 2: How to initialize a Map using key -> value notation
Elements of map2 = Map(GD -> Glazed Donut, VD -> Vanilla Donut)

3. How to access elements of Map by specific key

The code below shows how to access elements of Map by specific key.


println("\nStep 3: How to access elements of Map by specific key")
println(s"Element by key VD = ${map2("VD")}")
println(s"Element by key GD = ${map2("GD")}")

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


Step 3: How to access elements of Map by specific key
Element by key VD = Vanilla Donut
Element by key GD = Glazed Donut

 

4. How to add elements to Map using +=

The code below shows how to add elements to Map using +=.


println("\nStep 4: How to add elements to Map using +=")
map1 += ("KD" -> "Krispy Kreme Donut")
println(s"Element in map1 = $map1")

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

Step 4: How to add elements to Map using +=
Element in map1 = Map(CD -> Chocolate Donut, KD -> Krispy Kreme Donut, SD -> Strawberry Donut, PD -> Plain Donut)

 

5. How to add elements from a Map to an existing Map using ++=

The code below shows how to add elements from a Map to an existing Map using ++=.


println("\nStep 5: How to add elements from a Map to an existing Map using ++=")
map1 ++= map2
println(s"Elements in map1 = $map1")

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

Step 5: How to add elements from a Map to an existing Map using ++=
Elements in map1 = Map(GD -> Glazed Donut, CD -> Chocolate Donut, VD -> Vanilla Donut, KD -> Krispy Kreme Donut, SD -> Strawberry Donut, PD -> Plain Donut)

 

6. How to remove key and its value from Map using -=

The code below shows how to remove key and its value from Map using -=.


println("\nStep 6: How to remove key and its value from Map using -=")
map1 -= "CD"
println(s"Map without the key CD and its value = $map1")

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


Step 6: How to remove key and its value from Map using -=
Map without the key CD and its value = Map(GD -> Glazed Donut, VD -> Vanilla Donut, KD -> Krispy Kreme Donut, SD -> Strawberry Donut, PD -> Plain Donut)

 

7. How to initialize an empty Map

The code below shows how to initialize an empty Map.


println("\nStep 7: How to initialize an empty Map")
val emptyMap: Map[String,String] = Map.empty[String,String]
println(s"Empty Map = $emptyMap")

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


Step 7: How to initialize an empty Map
Empty Map = Map()

This concludes our tutorial on Learn How To Use Scala's Mutable Map 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 Map
  • How to initialize a Map using key -> value notation
  • How to access elements of Map by specific key
  • How to add elements to Map using +=
  • How to add elements from a Map to an existing Map using ++=
  • How to remove key and its value from Map using -=
  • How to initialize an empty Map

Tip

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 Scala's Mutable HashMap.

 

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Senior Software Developer | Elsevier
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on