Skip to content
Related Articles

Related Articles

Scala Set dropWhile() method with example
  • Last Updated : 15 Oct, 2019

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

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

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

Example #1:




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



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

Example #2:




// Scala program of dropWhile() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating a list 
        var s1 = Set(1, 3, 5, 4, 2
          
        // Print the set
        println(s1)
          
        // Applying dropWhile method 
        var res = s1.dropWhile(x => true
          
        // Displays output 
        println(res) 
      
    
Output:
Set(5, 1, 2, 3, 4)
Set()

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :