Skip to content
Related Articles

Related Articles

Scala SortedMap get() method with example
  • Last Updated : 02 Nov, 2019
GeeksforGeeks - Summer Carnival Banner

The get() method is utilized to give the value associated with the keys of the SortedMap. The values are returned here as an Option i.e, either in form of Some or None.

Method Definition:def get(key: A): Option[B]

Return Type: It returns the keys corresponding to the values given in the method as argument.

Example #1:




// Scala program of get()
// method
import scala.collection.immutable.SortedMap
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a SortedMap
        val m1 = SortedMap("geeks" -> 5, "for" -> 3, "cs" -> 2
          
        // Applying get method
        val result = m1.get("for")
          
        // Displays output
        println(result)
      
    }
}
Output:



Some(3)

Here, the key in the argument i.e, for is present in the SortedMap stated above so, the value of the key is returned in the Some form.
Example #2:




// Scala program of get()
// method
import scala.collection.immutable.SortedMap
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a SortedMap
        val m1 = SortedMap("geeks" -> 5, "for" -> 3, "cs" -> 2
          
        // Applying get method
        val result = m1.get("portal")
          
        // Displays output
        println(result)
      
    }
}
Output:
None

Here, the key in the argument is not present in the SortedMap so, None is returned.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :