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

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

Overview

In this tutorial, we will learn how to use Scala's Immutable SortedSet and perform common operations such as initialization, adding elements, adding SortedSetsSortedSet differences and intersection, ordering, and creating empty SortedSet.

 

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 SortedSet will be discussed in Chapter 8 on Collection Functions.

 

What is a SortedSet?

Following on from the previous tutorials on SetHashSet, and TreeSet, a SortedSet allows you to store unique elements but also provides ordering of the elements.

 

As per the Scala documentation, SortedSet is a trait. Its concrete class implementation is a TreeSet which we discussed in the previous tutorial.

 

As a reminder, since you are dealing with Set semantics, only distinct and non repeatable values will be stored.

 

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.immutable.SortedSet
println("Step 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 +")
val sortedSet2: SortedSet[String] = sortedSet1 + "Vanilla Donut" + "Vanilla Donut"
println(s"Adding elements to SortedSet using + = $sortedSet2")
// NOTE: we only have one Vanilla Donut element and not two as SortedSet is distinct

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


Step 3: How to add elements to SortedSet using +
Adding elements to SortedSet using + = 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 ++")
val sortedSet3: SortedSet[String] = sortedSet1 ++ SortedSet[String]("Vanilla Donut", "Glazed Donut")
println(s"Add two SortedSets together using ++ = $sortedSet3")

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


Step 4: How to add two SortedSets together using ++
Add two SortedSets together using ++ = TreeSet(Chocolate Donut, Glazed Donut, Plain Donut, Strawberry Donut, Vanilla Donut)

 

5. How to remove element in SortedSet using -

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


println("\nStep 5: How to remove element in SortedSet using -")
val sortedSet4: SortedSet[String] = sortedSet1 - "Plain Donut"
println(s"SortedSet without Plain Donut element = $sortedSet4")

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


Step 5: How to remove element in SortedSet using -
SortedSet without Plain Donut element = TreeSet(Chocolate Donut, Strawberry 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 sortedSet5: SortedSet[String] = SortedSet("Vanilla Donut", "Glazed Donut", "Plain Donut")
println(s"Intersection of sortedSet1 and sortedSet5 = ${sortedSet1 & sortedSet5}")

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(Plain 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 &~ sortedSet5}")

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 of SortedSet to descending alphabet

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


println("\nStep 8: How to change ordering of SortedSet to descending alphabet")
object AlphabetOrdering extends Ordering[String] {
 def compare(element1:String, element2:String) = element2.compareTo(element1)
}
val sortedSet6: SortedSet[String] = SortedSet("Plain Donut", "Strawberry Donut", "Chocolate Donut")(AlphabetOrdering)
println(s"Elements of sortedSet6 = $sortedSet6")
// NOTE: The elements are now printed in descending order first with Strawberry Donut, then Plain Donut and finally Chocolate Donut

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


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

NOTE:

  • The elements are now 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 Immutable 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 with 3 elements
  • 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 in SortedSet using -
  • How to find the intersection between two SortedSets using &
  • How to find the difference between two SortedSets using &~
  • How to change ordering of SortedSet to descending alphabet
  • 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 Immutable BitSet.

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: