Open In App

Scala Set find() method with example

Last Updated : 18 Oct, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
The find() method is utilized to find the first element of the set that satisfies the given predicate if present.
Method Definition: def find(p: (A) => Boolean): Option[A] Return Type: It returns the first element of the set which satisfies the given predicate.
Example #1: Scala
// Scala program of find() 
// method 

// Creating object 
object GfG 
{ 

    // Main method 
    def main(args:Array[String]) 
    { 
    
        // Creating a set 
        val s1 = Set(5, 12, 3, 13) 
        
        // Applying find method 
        val result = s1.find(_ == 3) 
        
        // Displays output 
        println(result) 
    
    } 
} 
Output:
Some(3)
Example #2: Scala
// Scala program of find() 
// method 

// Creating object 
object GfG 
{ 

    // Main method 
    def main(args:Array[String]) 
    { 
    
        // Creating a set 
        val s1 = Set(5, 12, 3, 13) 
        
        // Applying find method 
        val result = s1.find(_ == 10) 
        
        // Displays output 
        println(result) 
    
    } 
} 
Output:
None

Next Article

Similar Reads