Scala Map find() method with example
The find() method is utilized to find the first element of the map that satisfies the given predicate.
Method Definition: def find(p: ((A, B)) => Boolean): Option[(A, B)]
Return Type: It returns the first element of the map which satisfies the given predicate.
Example #1:
// Scala program of find() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating map val m1 = Map(3 -> "geeks", 1 -> "for", 2 -> "cs") // Applying find method val result = m1.find(_._2 == "for") // Displays output println(result) } } |
Some((1, for))
So, here the second pair which is the first pair that satisfies the given predicate so, the second pair is returned.
Example #2:
// Scala program of find() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating map val m1 = Map(3 -> "geeks", 1 -> "for", 2 -> "cs", 6 -> "geeks") // Applying find method val result = m1.find(_._2 == "geeks") // Displays output println(result) } } |
Some((3, geeks))
Recommended Posts:
- Scala Set find() method with example
- Scala Iterator find() method with example
- How to find largest key in Scala Map
- Find the last element of a list in scala
- Scala Set contains() method with example
- Scala Set sum() method with example
- Scala Set take() method with example
- Scala Set ++() method with example
- Scala Set -() method with example
- Scala map contains() method with example
- Scala Set +() method with example
- Scala Set min() method with example
- Scala Map max() method with example
- Scala Set &() method with example
- Scala Map last() 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.



