Scala SortedSet dropWhile() method with example

The dropWhile() method is utilized to drop the longest prefix of elements from the SortedSet that satisfies the stated condition.

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

Return Type: It returns a TreeSet containing all the elements after dropping the longest prefix of elements from the SortedSet that satisfies the stated condition.



Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of dropWhile() 
// method 
import scala.collection.immutable.SortedSet 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating a list 
        var s1 = SortedSet(1, 3, 5, 4, 2
          
        // Print the SortedSet
        println(s1)
          
        // Applying dropWhile method 
        var res = s1.dropWhile(x => {x % 2 != 0}) 
          
        // Displays output 
        println(res) 
      
    

chevron_right


Output:

TreeSet(1, 2, 3, 4, 5)
TreeSet(2, 3, 4, 5)

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of dropWhile() 
// method 
import scala.collection.immutable.SortedSet 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating a list 
        var s1 = SortedSet(15, 17, 21
          
        // Print the SortedSet
        println(s1)
          
        // Applying dropWhile method 
        var res = s1.dropWhile(x => {x % 3 == 0}) 
          
        // Displays output 
        println(res) 
      
    

chevron_right


Output:

TreeSet(15, 17, 21)
TreeSet(17, 21)


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.