Scala Stack exists() method with example
Last Updated :
03 Nov, 2019
In Scala Stack class
, the exists() method is utilized to check whether a predicate holds for any of the elements of the stack.
Method Definition: def exists(p: (A) => Boolean): Boolean
Return Type: It returns true if the predicate holds true for any of the elements of the stack or else returns false.
Example #1:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val s 1 = Stack( 1 , 3 , 2 , 7 , 6 , 5 )
println(s 1 )
val result = s 1 .exists(x => {x % 7 == 0 })
println( "Element divisible by 7 exists: " + result)
}
}
|
Output:
Stack(1, 3, 2, 7, 6, 5)
Element divisible by 7 exists: true
Example #2:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val s 1 = Stack( 1 , 3 , 2 , 7 , 6 , 5 )
println(s 1 )
val result = s 1 .exists(x => {x == 10 })
println( "Element 10 exists: " + result)
}
}
|
Output:
Stack(1, 3, 2, 7, 6, 5)
Element 10 exists: false
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...