Related Articles
Scala Stack equals() method with example
• Last Updated : 03 Nov, 2019

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:

 `// 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` `s``1` `=` `Stack(``1``, ``3``, ``2``, ``7``, ``6``, ``5``) ``         ` `        ``val` `s``2` `=` `Stack(``1``, ``3``, ``2``, ``7``, ``6``, ``5``)``         ` `        ``// Print the stack ``        ``println(``"Stack_1: "` `+ s``1``) ``           ` `        ``println(``"Stack_2: "` `+ s``2``)``           ` `        ``// Applying equals method  ``        ``val` `result ``=` `s``1``.equals(s``2``) ``         ` `        ``// Display output``        ``println(``"Stack_1 == Stack_2: "` `+ result)``    ``} ``} `
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:

 `// 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` `s``1` `=` `Stack(``1``, ``3``, ``2``, ``7``, ``6``, ``5``) ``         ` `        ``val` `s``2` `=` `Stack(``7``, ``3``, ``12``, ``7``, ``6``, ``5``)``         ` `        ``// Print the stack ``        ``println(``"Stack_1: "` `+ s``1``) ``           ` `        ``println(``"Stack_2: "` `+ s``2``)``           ` `        ``// Applying equals method  ``        ``val` `result ``=` `s``1``.equals(s``2``) ``         ` `        ``// Display output``        ``println(``"Stack_1 == Stack_2: "` `+ result)``    ``} ``} `
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>

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up