Home »
Scala
SortedMap empty() Method in Scala with Example
In this problem, we will learn about empty() method of SortedMap class. It is used to empty the given sortedMap. We will learn about empty() method with examples.
Submitted by Shivang Yadav, on December 20, 2020
Map is a collection that stores its elements as key-value pairs, like a dictionary. SortedMap is a special type of map in which elements are sorted in ascending order.
SortedMap empty() Method
SortedMap empty() method in Scala is used to convert the given sortedMap to an empty one i.e. delete all elements of SortedMap.
Syntax:
SortedMap_Name.empty
Parameter: The method does not accept any parameters.
Return: It returns a SortedMap which is empty.
Example: Program to illustrate the working of empty method
// Program to illustrate the working of empty() method in scala
import scala.collection.SortedMap
object myObject {
def main(args: Array[String]) {
val mySortedMap = SortedMap("scala" -> 5, "JavaScript" -> 6, "Ruby" -> 8)
println("Sorted Map: " + mySortedMap)
val emptySortedMap = mySortedMap.empty
println("Empty Sorted Map: " + emptySortedMap)
}
}
Output:
Sorted Map: TreeMap(JavaScript -> 6, Ruby -> 8, scala -> 5)
Empty Sorted Map: TreeMap()