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

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

Overview

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

 

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

 

What is a TreeSet?

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 TreeSet 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. A TreeSet is a class implementation of the SortedSet trait and it uses a Red Black Tree as its underlying data structure.

Steps

1. How to initialize a TreeSet with 3 elements

The code below shows how to initialize a TreeSet with 3 elements


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

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

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

 

2. How to check specific elements in TreeSet

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


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

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


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

 

3. How to add elements to TreeSet using +=

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


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

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


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

 

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

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


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

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

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

 

5. How to remove element from TreeSet using -=

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

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

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

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

 

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

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


println("\nStep 6: How to find the intersection between two TreeSets using &")
val treeSet2: TreeSet[String] = TreeSet("Vanilla Donut", "Glazed Donut", "Plain Donut")
println(s"Intersection of treeSet1 and treeSet2 = ${treeSet1 & treeSet2}")

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


Step 6: How to find the intersection between two TreeSets using &
Intersection of treeSet1 and treeSet2 = TreeSet(Glazed Donut, Vanilla Donut)

 

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

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


println("\nStep 7: How to find the difference between two TreeSets using &~")
println(s"Difference of treeSet1 and treeSet2 = ${treeSet1 &~ treeSet2}")

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


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

8. How to change ordering to descending alphabet in TreeSet

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


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

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


Step 8: How to change ordering to descending alphabet in TreeSet
Elements of treeSet3 = 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 TreeSet

The code below shows how to initialize an empty TreeSet.


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

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


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

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

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

 

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: