SortedMap in Scala

Scala | SortedMap: In this tutorial, we are going to learn about the sortedMap in Scala, its syntax and working examples.
Submitted by Shivang Yadav, on January 24, 2021

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]) {
        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.




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.