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)
}
}
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!