×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

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()

Comments and Discussions!

Load comments ↻





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