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




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.