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

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

Overview

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

 

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 HashMap will be discussed in Chapter 8 on Collection Functions.

 

What is a HashMap?

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

 

Steps

1. How to initialize a HashMap with 3 elements

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


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

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


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

 

2. How to initialize HashMap using key -> value notation

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


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

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

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

3. How to access elements of HashMap by specific key

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


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

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


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

 

4. How to add elements to HashMap using +=

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


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

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

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

 

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

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

println("\nStep 5: How to add elements from a HashMap to an existing HashMap using ++=")
hashMap1 ++= hashMap2
println(s"Elements in hashMap1 = $hashMap1")

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

Step 5: How to add elements from a HashMap to an existing HashMap using ++=
Elements in hashMap1 = 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 HashMap using -=

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


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

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


Step 6: How to remove key and its value from HashMap using -=
HashMap 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 HashMap

The code below shows how to initialize an empty HashMap.


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

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


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

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

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 ListMap.

 

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: