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

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

Overview

In this tutorial, we will learn how to use Scala's Immutable HashMap and perform common operations such as initialization, adding or removing elements and find elements by key within the HashMap.

 

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

Steps

1. How to initialize a HashMap with 3 elements using Tuples of key and value

The code below shows how to initialize a HashMap with 3 elements using Tuples of key and value.


import scala.collection.immutable.HashMap
println("Step 1: How to initialize a HashMap with 3 elements using Tuples of key and value")
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 using Tuples of key and value
Elements of hashMap1 = Map(PD -> Plain Donut, SD -> Strawberry Donut, CD -> Chocolate 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 in HashMap by specific key

The code below shows mow to access elements in HashMap by specific key.


println("\nStep 3: How to access elements in 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 in 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 +")
val hashMap3: HashMap[String, String] = hashMap1 + ("KD" -> "Krispy Kreme Donut")
println(s"Element in hashMap3 = $hashMap3")

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 hashMap3 = Map(PD -> Plain Donut, SD -> Strawberry Donut, KD -> Krispy Kreme Donut, CD -> Chocolate Donut)

 

5. How to add two HashMaps together using ++

The code below shows how to add two HashMaps together using ++


println("\nStep 5: How to add two HashMaps together using ++")
val hashMap4: Map[String, String] = hashMap1 ++ hashMap2
println(s"Elements in hashMap4 = $hashMap4")

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


Step 5: How to add two HashMaps together using ++
Elements in hashMap4 = 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 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 -")
val hashMap5: Map[String, String] = hashMap4 - ("CD")
println(s"HashMap without the key CD and its value = $hashMap5")

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(PD -> Plain Donut, GD -> Glazed Donut, SD -> Strawberry Donut, VD -> Vanilla 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 emptyHashMap: HashMap[String,String] = HashMap.empty[String,String]
println(s"Empty HashMap = $emptyHashMap")

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 Immutable 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 with 3 elements using Tuples of key and value
  • How to initialize HashMap using key -> value notation
  • How to access elements in HashMap by specific key
  • How to add elements to HashMap using +
  • How to add two HashMaps together 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 Immutable TreeMap.

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