Scala Queue forall() method with example
Last Updated :
24 Oct, 2019
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:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val q 1 = Queue( 1 , 3 , 2 , 7 , 6 , 5 )
println(q 1 )
val result = q 1 .forall(x => {x % 7 == 0 })
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:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val q 1 = Queue( 1 , 3 , 7 , 5 )
println(q 1 )
val result = q 1 .forall(x => {x % 2 ! = 0 })
print( "All the elements are odd: " + result)
}
}
|
Output:
Queue(1, 3, 7, 5)
All the elements are odd: true
Share your thoughts in the comments
Please Login to comment...