Scala Mutable SortedMap get() method with example
Last Updated :
04 May, 2020
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:
import scala.collection.SortedMap
object GfG
{
def main(args : Array[String])
{
val m 1 = SortedMap( "geeks" - > 5 , "for" - > 3 , "cs" - > 2 )
val result = m 1 .get( "for" )
println(result)
}
}
|
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:
import scala.collection.SortedMap
object GfG
{
def main(args : Array[String])
{
val m 1 = SortedMap( "geeks" - > 5 , "for" - > 3 , "cs" - > 2 )
val result = m 1 .get( "portal" )
println(result)
}
}
|
Here, the key in the argument is not present in the SortedMap so, None is returned.
Share your thoughts in the comments
Please Login to comment...