Open In App

Scala Stack ++ method with example

Last Updated : 02 Nov, 2019
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)


Previous Article
Next Article

Similar Reads

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
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 copy of the stack with an element prepended. Note that the ending operators are right associative. Method Definition - def +:(elem: A) Returns - A new stack consisting of elem followed by all elements of this stack. Examp
1 min read
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: // Scala program of mutable stack :+() method // Import Stack import scala
1 min read
Scala Stack /:() method with example
In Scala, scala.collection.mutable implements Stack data structure. The /: method applies a binary operator to a start value and all elements of this traversable or iterator, going left to right. Method Definition - def /:[B](z: B)(op: (B, A) ? B): B Returns - the result of inserting op between consecutive elements of this traversable or iterator.
1 min read
Scala Stack :() method with example
In Scala, scala.collection.mutable implements Stack data structure. The :\ method applies a binary operator to a start value and all elements of this traversable or iterator, going right to left. Method Definition - def :\[B](z: B)(op: (A, B) ? B): B Returns - the result of inserting op between consecutive elements of this traversable or iterator.
1 min read
Scala Stack push() method with example
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(a
1 min read
Scala Stack pop() method with example
In Scala Stack class, the pop() method is utilized to remove and return the element at the top of the stack. Method Definition: def pop(): A Return Type: It removes and returns the element at the top of the stack. Example #1: // Scala program of pop() // method import scala.collection.mutable.Stack // Creating object object GfG { // Main method def
2 min read
Scala Stack pushAll() method with example
In Scala Stack class, the pushAll() method is utilized to add the elements from a collection to a stack. Method Definition: def pushAll(elems: IterableOnce[A]): Stack.this.type Return Type: It returns a stack that contains all the elements of the given collection. Example #1: // Scala program of pushAll() // method import scala.collection.mutable.S
1 min read
Scala Stack contains() method with example
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 // C
1 min read
Scala Stack apply() method with example
In Scala Stack class, the apply() method is utilized to return an element at a given position in the stack. The top of the stack corresponds to the element at 0th position and so on. Method Definition: def apply(idx: Int): A Return Type: It returns an element at a given position in the stack. Example #1: // Scala program of apply() // method import
1 min read