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

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

Overview

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

 

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

 

What is a TreeSet?

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

 

As per the Scala documentation, TreeSet internally uses a Red-Back data structure to ensure a balanced tree that will provide O(log n) for most operations.

 

Since you are dealing with Set semantics, only distinct and non repeatable values will be stored.

 

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

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


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

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


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

 

5. How to remove element in TreeSet using -

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


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

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


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

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 treeSet5 = TreeSet(Plain 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 treeSet5 = ${treeSet1 &~ treeSet5}")

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 treeSet5 = TreeSet(Chocolate Donut, Strawberry Donut)

 

8. How to change ordering of TreeSet to descending alphabet

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


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

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


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

NOTE:

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

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