Open In App

Scala Stack ++ method with example

Improve
Improve
Like Article
Like
Save
Share
Report

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 elements of traversable.

Example #1:




// 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) 
          
    


Output:

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

Example #2:




// 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) 
          
    


Output:

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


Last Updated : 02 Nov, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads