Open In App

Scala Queue forall() method with example

Last Updated : 24 Oct, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The forall() method is utilized to check if a predicate holds true for all the elements of the queue.

Method Definition: def forall(p: (A) => Boolean): Boolean

Return Type: It returns true if the predicate holds true for all the elements of the queue or else returns false.

Example #1:




// Scala program of forall() 
// method 
  
// Import Queue  
import scala.collection.mutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating queues 
        val q1 = Queue(1, 3, 2, 7, 6, 5
          
        // Print the queue
        println(q1)
          
        // Applying forall method 
        val result = q1.forall(x => {x % 7 == 0}) 
          
        // Displays output 
        print("All the elements are divisible by 7: " + result)
    


Output:

Queue(1, 3, 2, 7, 6, 5)
All the elements are divisible by 7: false

Example #2:




// Scala program of forall() 
// method 
  
// Import Queue  
import scala.collection.mutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating queues 
        val q1 = Queue(1, 3, 7, 5
          
        // Print the queue
        println(q1)
          
        // Applying forall method 
        val result = q1.forall(x => {x % 2 != 0}) 
          
        // Displays output 
        print("All the elements are odd: " + result)
    


Output:

Queue(1, 3, 7, 5)
All the elements are odd: true


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads