Open In App
Related Articles

Scala Stack push() method with example

Improve Article
Improve
Save Article
Save
Like Article
Like

In Scala Stack class, the push() method is utilized to add an element on the top of the stack.

Method Definition: def push(elem: A): Stack.this.type

Return Type: It adds an element on the top of the stack.

Example #1:




// Scala program of push() 
// method 
  
import scala.collection.mutable.Stack 
  
// Creating object 
object GfG 
      
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a stack
        var s = Stack[Int]() 
  
        // Applying push method
        s.push(1)
        s.push(2
        s.push(3
        s.push(4)
          
        // Print the Stack
        println(s) 
  
    


Output:

Stack(4, 3, 2, 1)

Example #2:




// Scala program of push() 
// method 
  
import scala.collection.mutable.Stack 
  
// Creating object 
object GfG 
      
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a stack
        var s = Stack[String]() 
  
        // Applying push method
        s.push("C++")
        s.push("Java"
        s.push("Python"
        s.push("Scala")
          
        // Print the Stack
        println(s) 
  
    


Output:

Stack(Scala, Python, Java, C++)

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 03 Nov, 2019
Like Article
Save Article
Similar Reads