Scala Mutable SortedSet filter() method

In Scala mutable collections, SortedSet filter() method is utilized to select all elements of the SortedSet which satisfies a stated predicate.

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

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

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

TreeSet(3, 5)

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

TreeSet()



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.