Scala Tutorial - Learn How To Use Scala's Mutable Set

By Nadim Bahadoor | Last updated: February 3, 2020 at 7:32 am

Overview

In this tutorial, we will learn how to use Scala's Mutable Set to perform common operations such as initialize a Set, check specific elements, add and remove elements, Set intersection and difference, and create an 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 Mutable 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 elements which are not repeatable. A Set also does not guarantee the ordering of elements.

 

As per Scala Documentation, a Mutable Set is a generic trait to support set semantics and behaviour.

Steps

1. How to initialize a Set with 3 elements

The code below shows how to initialize a Set with 3 elements.


import scala.collection.mutable.Set
println("\nStep 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(Strawberry Donut, Plain Donut, Chocolate Donut)

 

2. How to check specific elements in Set

The code below shows how to check specific elements in Set


println("\nStep 2: How to check specific elements 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 in Set
Element Plain Donut = true
Element Strawberry Donut = true
Element Chocolate Donut = true

 

3. How to add elements to Set using +=

The code below shows how to add elements to Set using +=


println("\nStep 3: How to add elements to Set using +=")
set1 += "Vanilla Donut"
println(s"Elements of set1 after adding elements Vanilla Donut = $set1")

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


Step 3: How to add elements to Set using +=
Elements of set1 after adding elements Vanilla Donut = Set(Strawberry Donut, Plain Donut, Vanilla Donut, Chocolate Donut)

 

4. How to add all elements from another Set using ++=

The code below shows how to add all elements from another Set using ++=.


println("\nStep 4: How to add all elements from another Set using ++=")
set1 ++= Set[String]("Vanilla Donut", "Glazed Donut")
println(s"Elements of set1 after adding second set = $set1")

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

Step 4: How to add all elements from another Set using ++=
Elements of set1 after adding second set = Set(Strawberry Donut, Plain Donut, Glazed Donut, Vanilla Donut, Chocolate 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 -=")
set1 -= "Plain Donut"
println(s"Elements of set1 without Plain Donut element = $set1")

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

Step 5: How to remove element from Set using -=
Elements of set1 without Plain Donut element = Set(Strawberry Donut, Glazed Donut, Vanilla 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 set2: Set[String] = Set("Vanilla Donut", "Glazed Donut", "Plain Donut")
println(s"Intersection of set1 and set5 = ${set1 & set2}")

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(Glazed Donut, Vanilla 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 of set1 and set2 = ${set1 &~ set2}")

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 of set1 and set2 = 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()

This concludes our tutorial on Learn How To Use Scala's Mutable Set 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 Set
  • How to check specific elements in Set
  • How to add elements to Set using +=
  • How to add all elements from another Set 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

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 HashSet.

 

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Technology and Finance Consultant with over 14 years of hands-on experience building large scale systems in the Financial (Electronic Trading Platforms), Risk, Insurance and Life Science sectors. I am self-driven and passionate about Finance, Distributed Systems, Functional Programming, Big Data, Semantic Data (Graph) and Machine Learning.
Other allaboutscala.com tutorials you may like: