Scala Tutorial - Learn How To Use Scala's Mutable HashSet
Overview
In this tutorial, we will learn how to use Scala's Mutable HashSet to perform common operations such as initialize a HashSet, check specific elements, add and remove elements, HashSet intersection and difference, and create an empty HashSet.
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 HashSet will be discussed in Chapter 8 on Collection Functions.
What is a HashSet?
As per Wikipedia, a Set is a data structure which allows you to store elements which are not repeatable. A Set also does not guarantee the ordering of elements.
As per Scala Documentation, a Mutable HashSet is the concrete implementation of the mutable Set trait.
Steps
1. How to initialize a HashSet with 3 elements
The code below shows how to initialize a HashSet with 3 elements.
import scala.collection.mutable.HashSet
println("\nStep 1: How to initialize a HashSet with 3 elements")
val hashSet1: HashSet[String] = HashSet("Plain Donut","Strawberry Donut","Chocolate Donut")
println(s"Elements of hashSet1 = $hashSet1")
You should see the following output when you run your Scala application in IntelliJ:
Step 1: How to initialize a HashSet with 3 elements
Elements of hashSet1 = Set(Strawberry Donut, Plain Donut, Chocolate Donut)
2. How to check specific elements in HashSet
The code below shows how to check specific elements in HashSet
println("\nStep 2: How to check specific elements in HashSet")
println(s"Element Plain Donut = ${hashSet1("Plain Donut")}")
println(s"Element Strawberry Donut = ${hashSet1("Strawberry Donut")}")
println(s"Element Chocolate Donut = ${hashSet1("Chocolate Donut")}")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to check specific elements in HashSet
Element Plain Donut = true
Element Strawberry Donut = true
Element Chocolate Donut = true
3. How to add elements to HashSet using +=
The code below shows how to add elements to HashSet using +=
println("\nStep 3: How to add elements to HashSet using +=")
hashSet1 += "Vanilla Donut"
println(s"Elements of hashSet1 after adding Vanilla Donut element = $hashSet1")
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to add elements to HashSet using +=
Elements of hashSet1 after adding Vanilla Donut element = Set(Strawberry Donut, Plain Donut, Vanilla Donut, Chocolate Donut)
4. How to add two HashSets together using ++=
The code below shows how to add two HashSets together using ++=.
println("\nStep 4: How to add two HashSets together using ++=")
hashSet1 ++= HashSet[String]("Vanilla Donut", "Glazed Donut")
println(s"Elements of hashSet1 after adding another HashSet = $hashSet1")
You should see the following output when you run your Scala application in IntelliJ:
Step 4: How to add two HashSets together using ++=
Elements of hashSet1 after adding another HashSet = Set(Strawberry Donut, Plain Donut, Glazed Donut, Vanilla Donut, Chocolate Donut)
5. How to remove element from HashSet using -=
The code below shows how to remove element from HashSet using -=.
println("\nStep 5: How to remove element from HashSet using -=")
hashSet1 -= "Plain Donut"
println(s"HashSet without Plain Donut element = $hashSet1")
You should see the following output when you run your Scala application in IntelliJ:
Step 5: How to remove element from HashSet using -=
HashSet without Plain Donut element = Set(Strawberry Donut, Glazed Donut, Vanilla Donut, Chocolate Donut)
6. How to find the intersection between two HashSets using &
The code below shows how to find the intersection between two HashSets using &.
println("\nStep 6: How to find the intersection between two HashSet using &")
val hashSet2: HashSet[String] = HashSet("Vanilla Donut", "Glazed Donut", "Plain Donut")
println(s"Intersection of hashSet1 and hashSet2 = ${hashSet1 & hashSet2}")
You should see the following output when you run your Scala application in IntelliJ:
Step 6: How to find the intersection between two HashSets using &
Intersection of hashSet1 and hashSet2 = Set(Glazed Donut, Vanilla Donut)
7. How to find the difference between two HashSets using &~
The code below shows how to find the difference between two HashSets using &~.
println("\nStep 7: How to find the difference between two HashSets using &~")
println(s"Difference of hashSet1 and hashSet5 = ${hashSet1 &~ hashSet2}")
You should see the following output when you run your Scala application in IntelliJ:
Step 7: How to find the difference between two HashSets using &~
Difference of hashSet1 and hashSet5 = Set(Strawberry Donut, Chocolate Donut)
8. How to initialize an empty HashSet
The code below shows how to initialize an empty HashSet.
println("\nStep 8: How to initialize an empty HashSet")
val emptyHashSet: HashSet[String] = HashSet.empty[String]
println(s"Empty HashSet = $emptyHashSet")
You should see the following output when you run your Scala application in IntelliJ:
Step 8: How to initialize an empty HashSet
Empty HashSet = Set()
Summary
In this tutorial, we went over the following:
- How to initialize a HashSet
- How to check specific elements in HashSet
- How to add elements to HashSet using +=
- How to add two HashSets together using ++=
- How to remove element from HashSet using -=
- How to find the intersection between two HashSets using &
- How to find the difference between two HashSets using &~
- How to initialize an empty HashSet
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 mutable HashSet.
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 SortedSet.