Skip to content
Related Articles

Related Articles

Improve Article
Scala Set filter() method with example
  • Last Updated : 18 Oct, 2019

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
Recommended Articles
Page :