Skip to content
Related Articles

Related Articles

Scala List dropWhile() method with example
  • Last Updated : 26 Jul, 2019

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

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

Return Type: It returns all the elements of the list except the dropped ones.

Example #1:




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

Example #2:




// Scala program of dropWhile()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a list
        val m1 = List(3, 6, 5, 4, 2)
          
        // Applying dropWhile method
        val res = m1.dropWhile(x=>{x % 3 == 0})
          
        // Displays output
        println(res)
      
    }
}
Output:
List(5, 4, 2)



My Personal Notes arrow_drop_up
Recommended Articles
Page :