×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

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.

Comments and Discussions!

Load comments ↻





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