Scala Stack equals() method with example

In Scala Stack class, the equals() method is utilized to check if two stacks consist of the same elements in the same order.

Method Definition: def equals(o: Any): Boolean

Return Type: It returns true if both the stacks are same or else returns false.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of equals() 
// method 
  
// Import Stack 
import scala.collection.mutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating stacks  
        val s1 = Stack(1, 3, 2, 7, 6, 5
          
        val s2 = Stack(1, 3, 2, 7, 6, 5)
          
        // Print the stack 
        println("Stack_1: " + s1
            
        println("Stack_2: " + s2)
            
        // Applying equals method  
        val result = s1.equals(s2
          
        // Display output
        println("Stack_1 == Stack_2: " + result)
    

chevron_right


Output:



Stack_1: Stack(1, 3, 2, 7, 6, 5)
Stack_2: Stack(1, 3, 2, 7, 6, 5)
Stack_1 == Stack_2: true

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of equals() 
// method 
  
// Import Stack 
import scala.collection.mutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating stacks  
        val s1 = Stack(1, 3, 2, 7, 6, 5
          
        val s2 = Stack(7, 3, 12, 7, 6, 5)
          
        // Print the stack 
        println("Stack_1: " + s1
            
        println("Stack_2: " + s2)
            
        // Applying equals method  
        val result = s1.equals(s2
          
        // Display output
        println("Stack_1 == Stack_2: " + result)
    

chevron_right


Output:

Stack_1: Stack(1, 3, 2, 7, 6, 5)
Stack_2: Stack(7, 3, 12, 7, 6, 5)
Stack_1 == Stack_2: false

Stack class, the div>




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.