Scala Tutorial - Learn How To Use Scala's Immutable HashSet
Overview
In this tutorial, we will learn how to use Scala's Immutable HashSet and perform common operations such as initialization, adding elements, adding HashSets, HashSet differences and intersection, and creating 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 Immutable 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 some values but where the values cannot be repeatable.
In Scala, a HashSet is a concrete implementation of Set semantics. The HashSet will use the element's hashCode as a key to allow for fast lookup of the element's value within the HashSet.
The internal data structure of the HashSet in Scala is a HashTrie and this is different to a HashTable if you come from a Java background. You can learn more about HashTrie from Wikipedia.
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.immutable.HashSet
println("Step 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(Plain Donut, Chocolate Donut, Strawberry 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 in HashSet using +
The code below shows how to add elements in HashSet using +.
println("\nStep 3: How to add elements in HashSet using +")
val hashSet2: HashSet[String] = hashSet1 + "Vanilla Donut" + "Vanilla Donut"
println(s"Adding elements to HashSet using + = $hashSet2")
// NOTE: we only have one Vanilla Donut element and not two as HashSet is distinct
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to add elements in HashSet using +
Adding elements to HashSet using + = Set(Vanilla Donut, Plain Donut, Chocolate Donut, Strawberry 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 ++")
val hashSet3: HashSet[String] = hashSet1 ++ HashSet[String]("Vanilla Donut", "Glazed Donut")
println(s"Add two HashSets together using ++ = $hashSet3")
You should see the following output when you run your Scala application in IntelliJ:
Step 4: How to add two HashSets together using ++
Add two HashSets together using ++ = Set(Vanilla Donut, Plain Donut, Chocolate Donut, Strawberry Donut, Glazed Donut)
5. How to remove element in HashSet using -
The code below shows how to remove element in HashSet using -.
println("\nStep 5: How to remove element in HashSet using -")
val hashSet4: HashSet[String] = hashSet1 - "Plain Donut"
println(s"HashSet without Plain Donut element = $hashSet4")
You should see the following output when you run your Scala application in IntelliJ:
Step 5: How to remove element in HashSet using -
HashSet without Plain Donut element = Set(Chocolate Donut, Strawberry 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 HashSets using &")
val hashSet5: HashSet[String] = HashSet("Vanilla Donut", "Glazed Donut", "Plain Donut")
println(s"Intersection of hashSet1 and hashSet5 = ${hashSet1 & hashSet5}")
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 hashSet5 = Set(Plain 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 &~ hashSet5}")
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(Chocolate Donut, Strawberry 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 with 3 elements
- How to check specific elements in HashSet
- How to add elements in HashSet using +
- How to add two HashSets together using ++
- How to remove element in 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 immutable 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 Immutable TreeSet.