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

By Nadim Bahadoor | Last updated: February 3, 2020 at 7:22 am

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()

This concludes our tutorial on Learn How To Use Scala's Immutable 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 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

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.

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: