Home »
Scala
SortedMap in Scala
By IncludeHelp Last updated : November 14, 2024
SortedMap
SortedMap is a special type of map in which the elements are sorted based on mathematical ordering.
Syntax
storedMap_Name = SortedMap(key -> value, key -> value, ...)
Example
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)
}
}
Output
Sorted Map: TreeMap(JavaScript -> 6, Ruby -> 8, scala -> 5)
Explanation
In the above code, we have created the sortedMap in Scala using the sortedMap method with key-value pairs that are not then ordered based on mathematical ordering.