Scala Stack contains() method with example
Last Updated :
03 Nov, 2019
In Scala Stack class
, the contains() method is utilized to check if an element is present in the stack of not.
Method Definition: def contains(elem: A): Boolean
Return Type: It returns true if the element is present in the stack or else it returns false.
Example #1:
import scala.collection.mutable.Stack
object GfG
{
def main(args : Array[String])
{
val s 1 = Stack( 1 , 2 , 3 , 4 , 5 )
println(s 1 )
val result = s 1 .contains( 10 )
println( "Stack contains element 10: " + result)
}
}
|
Output:
Stack(1, 2, 3, 4, 5)
Stack contains element 10: false
Example #2:
import scala.collection.mutable.Stack
object GfG
{
def main(args : Array[String])
{
val s 1 = Stack( "C++" , "Java" , "Python" , "Scala" )
println(s 1 )
val result = s 1 .contains( "Scala" )
println( "Stack contains element Scala: " + result)
}
}
|
Output:
Stack(C++, Java, Python, Scala)
Stack contains element Scala: true
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...