Open In App

Scala Stack contains() method with example

Last Updated : 03 Nov, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:




// Scala program of contains() 
// method 
  
import scala.collection.mutable.Stack 
  
// Creating object 
object GfG 
      
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a stack
        val s1 = Stack(1, 2, 3, 4, 5
          
        // Print the stack
        println(s1)
      
        // Applying contains method    
        val result = s1.contains(10)
          
        // Display output
        println("Stack contains element 10: " + result) 
  
    


Output:

Stack(1, 2, 3, 4, 5)
Stack contains element 10: false

Example #2:




// Scala program of contains() 
// method 
  
import scala.collection.mutable.Stack 
  
// Creating object 
object GfG 
      
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a stack
        val s1 = Stack("C++", "Java", "Python", "Scala"
          
        // Print the stack
        println(s1)
      
        // Applying contains method    
        val result = s1.contains("Scala")
          
        // Display output
        println("Stack contains element Scala: " + result) 
  
    


Output:

Stack(C++, Java, Python, Scala)
Stack contains element Scala: true


Similar Reads

Scala map contains() method with example
The contains() method of Scala is equivalent to the isDefinedAt method of Scala but the only difference is that isDefinedAt is observed on all the PartialFunction classes while contains is clearly defined to the Map interface of Scala. It checks whether the stated map contains a binding for a key or not. Method Definition: def contains(key: K): Boo
2 min read
Scala List contains() method with example
The contains() method is utilized to check whether a certain element is present in the list or not. Method Definition: def contains(elem: Any): Boolean Return Type: It returns true if the element present in the contains method as argument is also present in the stated list else it returns false. Example #1: // Scala program of contains() // method
1 min read
Scala Set contains() method with example
The contains() method is utilized to check if an element is present in the set of not. Method Definition: def contains(elem: A): Boolean Return Type: It returns true if the element is present in the set else it returns false. Example #1: // Scala program of contains() // method // Creating object object GfG { // Main method def main(args:Array[Stri
1 min read
Scala Queue contains() method with example
The contains() method is utilized to test if an element is present in the queue or not. Method Definition: def contains[A1 >: A](elem: A1): Boolean Return Type: It returns true if the element is present in the queue, else it returns false. Example #1: // Scala program of contains() // method // Import Queue import scala.collection.mutable._ // C
1 min read
Scala SortedSet contains() method with example
The contains() method is utilized to check if an element is present in the SortedSet of not. Method Definition: def contains(elem: A): Boolean Return Type: It returns true if the element is present in the SortedSet else it returns false. Example #1: // Scala program of contains() // method import scala.collection.immutable.SortedSet // Creating obj
1 min read
Scala TreeSet contains() method with example
In Scala TreeSet class, the contains() method is utilized to check if an element is present in the TreeSet of not. Method Definition: def contains(elem: A): Boolean Return Type: It returns true if the element is present in the TreeSet or else it returns false. Example #1: // Scala program of contains() // method // Import TreeSet import scala.colle
1 min read
Scala SortedMap contains() method with example
The contains() method of Scala is equivalent to the isDefinedAt method of Scala but the only difference is that isDefinedAt is observed on all the PartialFunction classes while contains is clearly defined to the SortedMap interface of Scala. It checks whether the stated SortedMap contains a binding for a key or not. Method Definition: def contains(
2 min read
Scala Mutable SortedMap contains() method with example
The contains() method of Scala is equivalent to the isDefinedAt method of Scala but the only difference is that isDefinedAt is observed on all the PartialFunction classes while contains is clearly defined to the SortedMap interface of Scala. It checks whether the stated SortedMap contains a binding for a key or not. Method Definition: def contains(
2 min read
Scala Stack ++ method with example
In Scala, scala.collection.mutable implements Stack data structure. The ++ method gives a new stack with elements from the left hand operand followed by the elements from the right hand operand. Method Definition - def ++[B](that: GenTraversableOnce[B]): Stack[B] Returns - A new stack which contains all elements of this stack followed by all elemen
1 min read
Scala Stack ++:() method with example
In Scala, scala.collection.mutable implements Stack data structure. The ++: method is similar to ++ method in Stack which gives a new stack with elements from the left hand operand followed by the elements from the right hand operand. But with the difference that in ++:() right operand determines the type of the resulting collection rather than the
2 min read