How to use Scala SortedSet collection

 104 total views,  3 views today

Welcome to Scala series. In this tutorial, you will learn How to use Scala SortedSet collection with detail examples of each operation.

So Before learning Scala SortedSet, let us understand a few basics of Scala Set collection.

Scala Set

  • Set is a collection to store values that don’t have/allow duplicate values. Always Set will have unique elements.
  • Set is Iterable and inherits all functionality of the iterable interface.
  • There are two variants of Set in Scala
    • Immutable Set(default)
    • Mutable Set
  • By default, Scala will use Immutable Set. In any case, if a mutable Set is needed, then we need to explicitly import mutable Set.
  • Immutable Set allows us to apply a function on Set but resulting in the creation of a new Set every time. It doesn’t allow us to change the source. On the other hand, the mutable Set allows modifying the source itself.

I have written a separate post on Scala immutable and mutable collection. So please read before you proceed.

Let us now understand what is Scala SortedSet collection and how to use it.

Objective

  • What is a Sorted Set in Scala?
  • How to create a SortedSet?
  • How to get the size of a SortedSet?
  • How to access each elements?
  • How to iterate over a SortedSet.
  • How to get the head element?
  • How to get the tail element?
  • How to add an element?
  • How to remove an element?
  • How to add/merge multiple SortedSets?

Scala SortedSet

  • SortedSet is an interface or a trait that is based in Scala Set semantics but the main difference is that it sorts the element in the SortedSet.
  • It orders the elements.
  • SortedSet = Sort + Scala Set collection
  • Immutable SortedSet allows us to apply a function on SortedSet but resulting in the creation of a new SortedSet every time. It doesn’t allow us to change the source. On the other hand, the mutable SortedSet allows modifying the source itself.

Now you have learned SortedSet and let us now dive in to coding examples and how to use Scala SortedSet.

Create a SortedSet

Here I have created a SortedSet with default String values.When you print ,you can see elements are sorted ordered.

/**
 * How to use Scala SortedSet collection
 */
package com.scala

import scala.collection.immutable.SortedSet
import java.io.PrintWriter
import java.io.File
import java.io.FileWriter

object StaticReference {

  def main(args: Array[String]): Unit = {

    //Create aan immutable SortedSet
    var sortedSet = SortedSet("StaticReference", "tutorials", "Scala", "Java", "BigData")

    //print
    println(sortedSet)

  }
}

Output

TreeSet(BigData, Java, Scala, StaticReference, tutorials)

As you can see, elements are sorted alphabetically.

Get Size of a SortedSet

We can use size() operation on SortedSet to get the Size.

/**
 * How to use Scala SortedSet collection
 */
package com.scala

import scala.collection.immutable.SortedSet
import java.io.PrintWriter
import java.io.File
import java.io.FileWriter

object StaticReference {

  def main(args: Array[String]): Unit = {

    //Create aan immutable SortedSet
    var sortedSet = SortedSet("StaticReference", "tutorials", "Scala", "Java", "BigData")

    //print
    println(sortedSet.size)

  }
}

Output
5

Access each element of a SortedSet

The SortedSet is not an ordered collection. Hence we cannot access the element using the index. There is no such thing called index in SortedSet.

So to access the elements of SortedSet, we have to iterate over the elements using foreach.

Iterate the SortedSet

We will use foreach() to iterate over the elements of SortedSet.

/**
 * How to use Scala SortedSet collection
 */
package com.scala

import scala.collection.immutable.SortedSet
import java.io.PrintWriter
import java.io.File
import java.io.FileWriter

object StaticReference {

  def main(args: Array[String]): Unit = {

    //Create aan immutable SortedSet
    var sortedSet = SortedSet("StaticReference", "tutorials", "Scala", "Java", "BigData")

    //print
    sortedSet.foreach(println)

  }
}

Output

BigData
Java
Scala
StaticReference
tutorials

Get Head element

/**
 * How to use Scala SortedSet collection
 */
package com.scala

import scala.collection.immutable.SortedSet
import java.io.PrintWriter
import java.io.File
import java.io.FileWriter

object StaticReference {

  def main(args: Array[String]): Unit = {

    //Create aan immutable SortedSet
    var sortedSet = SortedSet("StaticReference", "tutorials", "Scala", "Java", "BigData")

    //print
    println(sortedSet.head)

  }
}

Output
BigData

Get Tail Elements

/**
 * How to use Scala SortedSet collection
 */
package com.scala

import scala.collection.immutable.SortedSet
import java.io.PrintWriter
import java.io.File
import java.io.FileWriter

object StaticReference {

  def main(args: Array[String]): Unit = {

    //Create aan immutable SortedSet
    var sortedSet = SortedSet("StaticReference", "tutorials", "Scala", "Java", "BigData")

    //print
    println(sortedSet.tail)

  }
}


Output
TreeSet(Java, Scala, StaticReference, tutorials)

 

Add an element

We have to use “+” operation add a new element to the SortedSet.

/**
 * How to use Scala SortedSet collection
 */
package com.scala

import scala.collection.immutable.SortedSet
import java.io.PrintWriter
import java.io.File
import java.io.FileWriter

object StaticReference {

  def main(args: Array[String]): Unit = {

    //Create aan immutable SortedSet
    var sortedSet = SortedSet("StaticReference", "tutorials", "Scala", "Java", "BigData")

    //print
    println(sortedSet.+("Anchor"))

  }
}

Output

TreeSet(Anchor, BigData, Java, Scala, StaticReference, tutorials)

Remove an element

We have to use “-” operation to remove an element from the SortedSet.

/**
 * How to use Scala SortedSet collection
 */
package com.scala

import scala.collection.immutable.SortedSet
import java.io.PrintWriter
import java.io.File
import java.io.FileWriter

object StaticReference {

  def main(args: Array[String]): Unit = {

    //Create aan immutable SortedSet
    var sortedSet = SortedSet("StaticReference", "tutorials", "Scala", "Java", "BigData")

    //print
    println(sortedSet.-("Java"))

  }
}

Output

TreeSet(BigData, Scala, StaticReference, tutorials)

Adding multiple SortedSets

We have to use “++” operation to add multiple SortedSet together.

/**
 * How to use Scala SortedSet collection
 */
package com.scala

import scala.collection.immutable.SortedSet
import java.io.PrintWriter
import java.io.File
import java.io.FileWriter

object StaticReference {

  def main(args: Array[String]): Unit = {

    //Create aan immutable SortedSet
    var sortedSet = SortedSet("StaticReference", "tutorials", "Scala", "Java", "BigData")

    var sortedSet1 = SortedSet("feedback", "comment", "like")

    var sortedSet2 = SortedSet("share")

    //print
    println(sortedSet.++(sortedSet1).++(sortedSet2))

  }
}

Output

TreeSet(BigData, Java, Scala, StaticReference, comment, feedback, like, share, tutorials)

References: 

I hope you like this tutorial and you were able to understand How to use Scala SortedSet collection.

Thanks for reading and please give us a thumb up and comment below!

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.