Skip to content
Related Articles

Related Articles

Scala Map get() method with example
  • Last Updated : 13 Aug, 2019

The get() method is utilized to give the value associated with the keys of the map. 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:



Some(3)

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

None

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :