Home »
Scala
SortedMap empty() Method in Scala
By IncludeHelp Last updated : November 14, 2024
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
Parameters
The method does not accept any parameters.
Return Type
It returns a SortedMap which is empty.
Example 1
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]): Unit = {
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()