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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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



My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

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.