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

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

Overview

In this tutorial, we will learn how to use Scala's Immutable TreeMap and perform common operations such as initialization, adding or removing elements, find elements by key within the TreeMap and sorting.

 

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

Steps

1. How to initialize a TreeMap with 3 elements using Tuples key and value

The code below shows how to initialize a TreeMap with 3 elements using Tuples key and value.


import scala.collection.immutable.TreeMap
println("Step 1: How to initialize a TreeMap with 3 elements using Tuples key and value")
val treeMap1: TreeMap[String, String] = TreeMap(("PD","Plain Donut"),("SD","Strawberry Donut"),("CD","Chocolate Donut"))
println(s"Elements of treeMap1 = $treeMap1")

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


Step 1: How to initialize a TreeMap with 3 elements using Tuples key and value
Elements of treeMap1 = Map(CD -> Chocolate Donut, PD -> Plain Donut, SD -> Strawberry Donut)

 

NOTE:

  • The TreeMap is sorted by default using the natural order the its key.
  • In the above example, key CD is printed first followed by PD and then SD.

2. How to initialize TreeMap using key -> value notation

The code below shows how to initialize TreeMap using key -> value notation.


println("\nStep 2: How to initialize TreeMap using key -> value notation")
val treeMap2: TreeMap[String, String] = TreeMap("VD"-> "Vanilla Donut", "GD" -> "Glazed Donut")
println(s"Elements of treeMap2 = $treeMap2")

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


Step 2: How to initialize ListMap using key -> value notation
Elements of treeMap2 = Map(GD -> Glazed Donut, VD -> Vanilla Donut)

 

3. How to access elements of TreeMap by specific key

The code below shows how to access elements of TreeMap by specific key.


println("\nStep 3: How to access elements of TreeMap by specific key")
println(s"Element by key VD = ${treeMap2("VD")}")
println(s"Element by key GD = ${treeMap2("GD")}")

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


Step 3: How to access elements of TreeMap by specific key
Element by key VD = Vanilla Donut
Element by key GD = Glazed Donut

 

4. How to add elements to TreeMap using +

The code below shows how to add elements to TreeMap using +


println("\nStep 4: How to add elements to TreeMap using +")
val treeMap3: TreeMap[String, String] = treeMap1 + ("KD" -> "Krispy Kreme Donut")
println(s"Elements in treeMap3 = $treeMap3")

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


Step 4: How to add elements using +
Element in treeMap3 = Map(CD -> Chocolate Donut, KD -> Krispy Kreme Donut, PD -> Plain Donut, SD -> Strawberry Donut)

 

5. How to add two TreeMaps together using ++

The code below shows how to add two TreeMaps together using ++


println("\nStep 5: How to add two TreeMaps together using ++")
val treeMap4: TreeMap[String, String] = treeMap1 ++ treeMap2
println(s"Elements in treeMap4 = $treeMap4")

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


Step 5: How to add two TreeMaps together using ++
Elements in treeMap4 = Map(CD -> Chocolate Donut, GD -> Glazed Donut, PD -> Plain Donut, SD -> Strawberry Donut, VD -> Vanilla Donut)

 

6. How to remove key and its value from TreeMap using -

The code below shows how to remove key and its value from TreeMap using -


println("\nStep 6: How to remove key and its value from TreeMap using -")
val treeMap5: TreeMap[String, String] = treeMap4 - ("CD")
println(s"TreeMap without the key CD and its value = $treeMap5")

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


Step 6: How to remove key and its value from TreeMap using -
TreeMap without the key CD and its value = Map(GD -> Glazed Donut, PD -> Plain Donut, SD -> Strawberry Donut, VD -> Vanilla Donut)

 

7. How to change ordering of TreeMap to descending alphabet

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


println("\nStep 7: How to change ordering of TreeMap to descending alphabet")
object AlphabetOrdering extends Ordering[String] {
 def compare(key1:String, key2:String) = key2.compareTo(key1)
}
val treeMap6: TreeMap[String, String] = TreeMap(("PD","Plain Donut"),("SD","Strawberry Donut"),("CD","Chocolate Donut"))(AlphabetOrdering)
println(s"Elements of treeMap6 in descending order = $treeMap6")

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


Step 7: How to change ordering of TreeMap to descending alphabet
Elements of treeMap6 = Map(SD -> Strawberry Donut, PD -> Plain Donut, CD -> Chocolate Donut)

 

8. How to initialize an empty TreeMap

The code below shows how to initialize an empty TreeMap.


println("\nStep 8: How to initialize an empty TreeMap")
val emptyTreeMap: TreeMap[String,String] = TreeMap.empty[String,String]
println(s"Empty TreeMap = $emptyTreeMap")

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


Step 8: How to initialize an empty TreeMap
Empty TreeMap = Map()

This concludes our tutorial on Learn How To Use Scala's Immutable TreeMap 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 TreeMap with 3 elements using Tuples key and value
  • How to initialize TreeMap using key -> value notation
  • How to access elements of TreeMap by specific key
  • How to add elements to TreeMap using +
  • How to add two TreeMaps together using ++
  • How to remove key and its value from TreeMap using -
  • How to change ordering of TreeMap to descending alphabet
  • How to initialize an empty TreeMap

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

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