Scala Tutorial - Learn How To Use Scala's Immutable Map
Overview
In this tutorial, we will learn how to use Scala's Immutable Map and perform common operations such as initialization, adding or removing elements and find elements by key within the 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 Immutable Map will be discussed in Chapter 8 on Collection Functions.
Steps
1. How to initialize a Map with 3 elements using Tuples of key and value
The code below shows how to initialize a Map with 3 elements using Tuples of key and value.
println("Step 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(PD -> Plain Donut, SD -> Strawberry Donut, CD -> Chocolate Donut)
2. How to initialize Map using key -> value notation
The code below shows how to initialize Map using key -> value notation.
println("\nStep 2: How to initialize Map using key -> value notation")
val map2: Map[String, String] = Map("VD"-> "Vanilla Donut", "GD" -> "Glazed Donut")
println(s"Elements of map1 = $map2")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to initialize Map using key -> value notation
Elements of map1 = Map(VD -> Vanilla Donut, GD -> Glazed Donut)
3. How to access elements by specific key
The code below shows how to access elements by specific key
println("\nStep 3: How to access elements 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 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 using +
println("\nStep 4: How to add elements using +")
val map3: Map[String, String] = map1 + ("KD" -> "Krispy Kreme Donut")
println(s"Element in map3 = $map3")
You should see the following output when you run your Scala application in IntelliJ:
Step 4: How to add elements using +
Element in map3 = Map(PD -> Plain Donut, SD -> Strawberry Donut, CD -> Chocolate Donut, KD -> Krispy Kreme Donut)
5. How to add two Maps together using ++
The code below shows how to add two Maps together using ++
println("\nStep 5: How to add two Maps together using ++")
val map4: Map[String, String] = map1 ++ map2
println(s"Elements in map4 = $map4")
You should see the following output when you run your Scala application in IntelliJ:
Step 5: How to add two Maps together using ++
Elements in map4 = Map(PD -> Plain Donut, GD -> Glazed Donut, SD -> Strawberry Donut, VD -> Vanilla Donut, CD -> Chocolate 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 -")
val map5: Map[String, String] = map4 - ("CD")
println(s"Map without the key CD and its value = $map5")
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(PD -> Plain Donut, GD -> Glazed Donut, SD -> Strawberry Donut, VD -> Vanilla Donut)
7. How to initialize an empty Map
The code below shows how to 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 with 3 elements using Tuples of key and value
- How to initialize Map using key -> value notation
- How to access elements by specific key
- How to add elements to Map using +
- How to add two Maps together 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 immutable Map.
- If you are not familiar with Tuples as used in Step 1 above, feel free to review the tutorial on Learn How To Use Tuples.
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 Immutable HashMap.