Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Scala Set filter() method with example

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The filter() method is utilized to select all elements of the set which satisfies a stated predicate.

Method Definition: def filter(p: (A) => Boolean): Set[A]

Return Type: It returns a set containing all the elements of the set which satisfies the given predicate.

Example #1:




// Scala program of filter() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating a set 
        val s1 = Set(5, 12, 3, 13
          
        // Applying filter method 
        val result = s1.filter(_ < 10
          
        // Displays output 
        println(result) 
      
    

Output:

Set(5, 3)

Example #2:




// Scala program of filter() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating a set 
        val s1 = Set(5, 12, 3, 13
          
        // Applying filter method 
        val result = s1.filter(_ < 3
          
        // Displays output 
        println(result) 
      
    

Output:

Set()

My Personal Notes arrow_drop_up
Last Updated : 18 Oct, 2019
Like Article
Save Article
Similar Reads