The apply() is utilized to search a key in the stated map.
Method Definition: m1.apply(“key”)
Here m1 is map.
Return Type: It returns the value of the key to be searched.
Example #1:
object GfG
{
def main(args : Array[String])
{
val m 1 = Map( "geeks" - > 5 , "for" - > 3 , "geeks" - > 5 )
val result = m 1 .apply( "for" )
println(result)
}
}
|
Example #2:
object GfG
{
def main(args : Array[String])
{
val m 1 = Map( "geeks" - > 5 , "for" - > 3 , "geeks" - > 1 )
val result = m 1 .apply( "geeks" )
println(result)
}
}
|
Here, identical keys are present with different values so, the value of the last one is returned.