Scala Map find() method with example
Last Updated :
13 Aug, 2019
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:
object GfG
{
def main(args : Array[String])
{
val m 1 = Map( 3 - > "geeks" , 1 - > "for" , 2 - > "cs" )
val result = m 1 .find( _ . _ 2 == "for" )
println(result)
}
}
|
So, here the second pair which is the first pair that satisfies the given predicate so, the second pair is returned.
Example #2:
object GfG
{
def main(args : Array[String])
{
val m 1 = Map( 3 - > "geeks" , 1 - > "for" , 2 - > "cs" , 6 - > "geeks" )
val result = m 1 .find( _ . _ 2 == "geeks" )
println(result)
}
}
|
Share your thoughts in the comments
Please Login to comment...