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

By Nadim Bahadoor | Last updated: July 25, 2017 at 14:50 pm

Overview

In this tutorial, we will learn how to use Scala's Mutable SortedSet to perform common operations such as initialize a SortedSet, check specific elements, add and remove elements, SortedSet intersection and difference, ordering of elements, and create an empty SortedSet.

 

More advanced functions such as aggregate, fold, reduce, map, flatMap etc on the Mutable SortedSet will be discussed in Chapter 8 on Collection Functions.

 

What is a SortedSet?

As per Wikipedia, a Set is a data structure which allows you to store elements which are not repeatable. While a Set also does not guarantee the ordering of elements, a SortedSet will produce elements in a given order.

 

As per Scala Documentation, a SortedSet is a trait which provides the Set semantics but also allows you to drive the ordering of the elements within the SortedSet.

Steps

1. How to initialize a SortedSet with 3 elements

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


import scala.collection.mutable.SortedSet
println("\nStep 1: How to initialize a SortedSet with 3 elements")
val sortedSet1: SortedSet[String] = SortedSet("Plain Donut","Strawberry Donut","Chocolate Donut")
println(s"Elements of sortedSet1 = $sortedSet1")

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

Step 1: How to initialize a SortedSet with 3 elements
Elements of sortedSet1 = TreeSet(Chocolate Donut, Plain Donut, Strawberry Donut)

 

2. How to check specific elements in SortedSet

The code below shows how to check specific elements in SortedSet.


println("\nStep 2: How to check specific elements in SortedSet")
println(s"Element Plain Donut = ${sortedSet1("Plain Donut")}")
println(s"Element Strawberry Donut = ${sortedSet1("Strawberry Donut")}")
println(s"Element Chocolate Donut = ${sortedSet1("Chocolate Donut")}")

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


Step 2: How to check specific elements in SortedSet
Element Plain Donut = true
Element Strawberry Donut = true
Element Chocolate Donut = true

 

3. How to add elements to SortedSet using +=

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


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

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


Step 3: How to add elements to SortedSet using +=
Elements of sortedSet1 after adding Vanilla Donut element = TreeSet(Chocolate Donut, Plain Donut, Strawberry Donut, Vanilla Donut)

 

4. How to add two SortedSets together using ++=

The code below shows how to add two SortedSets together using ++=.


println("\nStep 4: How to add two SortedSets together using ++=")
sortedSet1 ++= SortedSet[String]("Vanilla Donut", "Glazed Donut")
println(s"Elements of sortedSet1 after adding second SortedSet = $sortedSet1")

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

Step 4: How to add two SortedSets together using ++=
Elements of sortedSet1 after adding second SortedSet = TreeSet(Chocolate Donut, Glazed Donut, Plain Donut, Strawberry Donut, Vanilla Donut)

 

5. How to remove element from SortedSet using -=

The code below shows how to remove element from SortedSet using -=.

println("\nStep 5: How to remove element from SortedSet using -=")
sortedSet1 -= "Plain Donut"
println(s"sortedSet1 without Plain Donut element = $sortedSet1")

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

Step 5: How to remove element from SortedSet using -=
sortedSet1 without Plain Donut element = TreeSet(Chocolate Donut, Glazed Donut, Strawberry Donut, Vanilla Donut)

 

6. How to find the intersection between two SortedSets using &

The code below shows how to find the intersection between two SortedSets using &.


println("\nStep 6: How to find the intersection between two SortedSets using &")
val sortedSet2: SortedSet[String] = SortedSet("Vanilla Donut", "Glazed Donut", "Plain Donut")
println(s"Intersection of sortedSet1 and sortedSet5 = ${sortedSet1 & sortedSet2}")

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


Step 6: How to find the intersection between two SortedSets using &
Intersection of sortedSet1 and sortedSet5 = TreeSet(Glazed Donut, Vanilla Donut)

 

7. How to find the difference between two SortedSets using &~

The code below shows how to find the difference between two SortedSets using &~.


println("\nStep 7: How to find the difference between two SortedSets using &~")
println(s"Difference of sortedSet1 and sortedSet5 = ${sortedSet1 &~ sortedSet2}")

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


Step 7: How to find the difference between two SortedSets using &~
Difference of sortedSet1 and sortedSet5 = TreeSet(Chocolate Donut, Strawberry Donut)

8. How to change ordering to descending alphabet in SortedSet

The code below shows how to change ordering to descending alphabet in SortedSet.


println("\nStep 8: How to change ordering to descending alphabet in SortedSet")
object DescendingAlphabetOrdering extends Ordering[String] {
 def compare(element1:String, element2:String) = element2.compareTo(element1)
}
val sortedSet6: SortedSet[String] = SortedSet("Plain Donut", "Strawberry Donut", "Chocolate Donut")(DescendingAlphabetOrdering)
println(s"Elements of sortedSet6 = $sortedSet6")

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


Step 8: How to change ordering to descending alphabet in SortedSet
Elements of sortedSet6 = TreeSet(Strawberry Donut, Plain Donut, Chocolate Donut)

NOTE:

  • The elements are printed in descending order first with Strawberry Donut, then Plain Donut and finally Chocolate Donut

9. How to initialize an empty SortedSet

The code below shows how to initialize an empty SortedSet.


println("\nStep 9: How to initialize an empty SortedSet")
val emptySortedSet: SortedSet[String] = SortedSet.empty[String]
println(s"Empty SortedSet = $emptySortedSet")

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


Step 9: How to initialize an empty SortedSet
Empty SortedSet = TreeSet()

This concludes our tutorial on Learn How To Use Scala's Mutable SortedSet 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 SortedSet
  • How to check specific elements in SortedSet
  • How to add elements to SortedSet using +=
  • How to add two SortedSets together using ++=
  • How to remove element from SortedSet using -=
  • How to find the intersection between two SortedSets using &
  • How to find the difference between two SortedSets using &~
  • How to change ordering to descending alphabet in SortedSet
  • How to initialize an empty SortedSet

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

 

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Senior Software Developer | Nephila Capital
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on