Scala Map get() method with example
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:
// 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) } } |
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:
// 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) } } |
None
Here, the key in the argument is not present in the map so, None is returned.
Recommended Posts:
- Scala map contains() method with example
- Scala Map max() method with example
- Scala Map last() method with example
- Scala Map min() method with example
- Scala Map take() method with example
- Scala | map() method
- Scala Map transform() method with example
- Scala Iterator next() method with example
- Scala Map toSet() method with example
- Scala Map tail() method with example
- Scala | flatMap Method
- Scala Iterator sum() method with example
- Scala Map takeRight() method with example
- Scala Map isEmpty() method with example
- Scala Map remove() method with example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



