Scala Tutorial - Learn How To Use Scala's Immutable HashMap
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.
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 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 HashMap together using ++
The code below shows how to add two HashMap together using ++
println("\nStep 5: How to add two HashMap 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 HashMap 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()
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
- 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 TreeMap.