Scala Tutorial - Learn How To Use Scala's Mutable Map
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.
And, don't forget to review the Data Structures tutorial before delving into Scala's Immutable and Mutable collections.
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()
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
- Don't forget to review Chapter 8 on Collection Functions where we will go over the rich suite of functions exposed on collections such as aggregate, collect, fold, reduce, map, flatMap, scan, partition etc
- Scala's documentation on mutable Map.
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.