SortedMap tail() Method in Scala with Example

Here, we will learn about the tail() method in Scala. The tail() method prints all elements of the sortedMap instead of the first elements. Here, we see syntax and examples to understand it.
Submitted by Shivang Yadav, on January 24, 2021

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

tail() method in Scala is used to print all elements of the sortedMap instead of the first element of the sorted map.

Syntax:

sortedMap.tail

Parameter: This method does not accept any parameter.

Return: It returns a map that contains all elements of the sortedMap instead of the first one.

Example 1: Program to illustrate the working of tail method in Scala

import scala.collection.immutable.SortedMap 

object myObject {
    def main(args: Array[String]) {
        val mySortedMap = SortedMap("scala" -> 5, "JavaScript" -> 6, "Ruby" -> 8) 
        val last = mySortedMap.tail
    
        println("The tail of the sortedMap is " + last) 
    }
}

Output:

The tail of the sortedMap is TreeMap(Ruby -> 8, scala -> 5)



Comments and Discussions!

Load comments ↻






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