Scala Stack :+() method with example

In Scala, scala.collection.mutable implements Stack data structure. The :+ method is used to create a copy of this stack with an element appended.

Method Definition – def :+(elem: A)

Returns – a new stack consisting of all elements of this stack followed by elem.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of mutable stack :+() method 
  
// Import Stack 
import scala.collection.mutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
        // Creating a stack 
        val q2 = List(11, 12, 13, 14, 15
          
        // Applying :+() method 
        val result = q2 :+ 100
              
        // Display output 
        print(result) 
          
    

chevron_right


Output:

List(11, 12, 13, 14, 15, 100)

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of mutable stack /:() 
// method 
  
// Import Stack 
import scala.collection.mutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
          
        val st1 = Stack(1, 2, 3)
        val st2 = Stack("Geeks", "For")
          
          
        // Applying :+() method 
        val result1 = st2 :+ st1
          
          
        // Display output 
        print(result1)
          
    

chevron_right


Output:

Stack(Geeks, For, Stack(1, 2, 3))



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.