Scala Tutorial - Learn How To Use Scala's Immutable Set
Overview
In this tutorial, we will learn how to use Scala's Immutable Set and perform common operations such as initialization, adding elements, adding Sets, Set differences and intersection, and creating empty Set.
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 Set will be discussed in Chapter 8 on Collection Functions.
What is a Set?
As per Wikipedia, a Set is a data structure which allows you to store some values but where the values cannot be repeatable.
Steps
1. How to initialize a Set with 3 elements
The code below shows how to initialize a Set with 3 elements.
println("Step 1: How to initialize a Set with 3 elements")
val set1: Set[String] = Set("Plain Donut","Strawberry Donut","Chocolate Donut")
println(s"Elements of set1 = $set1")
You should see the following output when you run your Scala application in IntelliJ:
Step 1: How to initialize a Set with 3 elements
Elements of set1 = Set(Plain Donut, Strawberry Donut, Chocolate Donut)
2. How to check specific elements exists in Set
The code below shows how to check specific elements exists in Set.
println("\nStep 2: How to check specific elements exists in Set")
println(s"Element Plain Donut = ${set1("Plain Donut")}")
println(s"Element Strawberry Donut = ${set1("Strawberry Donut")}")
println(s"Element Chocolate Donut = ${set1("Chocolate Donut")}")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to check specific elements exists in Set
Element Plain Donut = true
Element Strawberry Donut = true
Element Chocolate Donut = true
3. How to add elements in Set using +
The code below shows how to add elements in Set using +.
println("\nStep 3: How to add elements in Set using +")
val set2: Set[String] = set1 + "Vanilla Donut" + "Vanilla Donut"
println(s"Adding elements to Set using + = $set2")
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to add elements in Set using +
Adding elements to Set using + = Set(Plain Donut, Strawberry Donut, Chocolate Donut, Vanilla Donut)
NOTE:
- We only have one Vanilla Donut element as Set only allows distinct values.
4. How to add two Sets together using ++
The code below shows how to add two Sets together using ++.
println("\nStep 4: How to add two Sets together using ++")
val set3: Set[String] = set1 ++ Set[String]("Vanilla Donut", "Glazed Donut")
println(s"Add two Sets together using ++ = $set3")
You should see the following output when you run your Scala application in IntelliJ:
Step 4: How to add two Sets together using ++
Add two Sets together using ++ = Set(Vanilla Donut, Plain Donut, Chocolate Donut, Strawberry Donut, Glazed Donut)
5. How to remove element from Set using -
The code below shows how to remove element from Set using -.
println("\nStep 5: How to remove element from Set using -")
val set4: Set[String] = set1 - "Plain Donut"
println(s"Set without Plain Donut element = $set4")
You should see the following output when you run your Scala application in IntelliJ:
Step 5: How to remove element from Set using -
Set without Plain Donut element = Set(Strawberry Donut, Chocolate Donut)
6. How to find the intersection between two Sets using &
The code below shows how to find the intersection between two Sets using &.
println("\nStep 6: How to find the intersection between two Sets using &")
val set5: Set[String] = Set("Vanilla Donut", "Glazed Donut", "Plain Donut")
println(s"Intersection of set1 and set5 = ${set1 & set5}")
You should see the following output when you run your Scala application in IntelliJ:
Step 6: How to find the intersection between two Sets using &
Intersection of set1 and set5 = Set(Plain Donut)
7. How to find the difference between two Sets using &~
The code below shows how to find the difference between two Sets using &~
println("\nStep 7: How to find the difference between two Sets using &~")
println(s"Difference between set1 and set5 = ${set1 &~ set5}")
You should see the following output when you run your Scala application in IntelliJ:
Step 7: How to find the difference between two Sets using &~
Difference between set1 and set5 = Set(Strawberry Donut, Chocolate Donut)
8. How to initialize an empty Set
The code below shows how to initialize an empty Set.
println("\nStep 8: How to initialize an empty Set")
val emptySet: Set[String] = Set.empty[String]
println(s"Empty Set = $emptySet")
You should see the following output when you run your Scala application in IntelliJ:
Step 8: How to initialize an empty Set
Empty Set = Set()
Summary
In this tutorial, we went over the following:
- How to initialize a Set with 3 elements
- How to check specific elements exists in Set
- How to add elements in Set using +
- How to add two Sets together using ++
- How to remove element from Set using -
- How to find the intersection between two Sets using &
- How to find the difference between two Sets using &~
- How to initialize an empty Set
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 Set.
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 HashSet.