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 left one.

Method Definition – def ++:[B >: A, That](that: collection.Traversable[B])

Returns – A new stack which contains all elements of this stack followed by all elements of traversable.

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 q1 = Stack(1, 2, 3, 4, 5
          
        val q2 = Stack(11, 12, 13, 14, 15
          
        // Applying ++() method 
        val result = q1.++:(q2
              
        // Display output 
        print(result) 
          
    

chevron_right


Output:

Stack(11, 12, 13, 14, 15, 1, 2, 3, 4, 5)

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]) 
    
        // Creating a stack 
        val q1 = List(1, 2, 3)
          
        val q2 = Stack("for", "geeks"
          
        // Applying ++() method 
        val result = q1 ++: q2
              
        // Display output 
        print(result) 
          
    

chevron_right


Output:

Stack(1, 2, 3, for, geeks)



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.