Scala Stack operations explained

14 total views, 1 views today

Welcome to Scala series. In this tutorial, you will learn about the Scala Stack and I have explained most of the Stack operations with examples.

A stack is based on the Last-In-First-Out algorithm(LIFO) and it is one of the basic collections being used in any programming language. Scala does support Stack out of the box.

Some of the real-world use cases

  • Undo operation in windows (CTRL+Z)
  • Stack of plates
  • Browser Back button and so on..

Okay, Let us now deep dive into Scala Stack

Scala Stack

Scala Stack is based on the LIFO pattern. Stack allows us to push elements and pop(remove) elements from the top. i.e., Last pushed element will always be removed first.

The two main operations of a Scala Stack or in any programming language are,

  1. push (add elements)
  2. pop (remove elements from the top)

Scala allows us to implement both mutable and immutable stack. If you want to learn about the mutable and immutable collection, please visit here.

For this tutorial, we concentrate only on the mutable stack. To import or use any mutable collection, you have to explicitly import the below package

import scala.collection.mutable.Stack

Note: Immutable collection will not modify the source. Any changes to the immutable collection have to be captured by another collection.

So, let us get started with the various operations of the Scala Stack.

1. Create a Stack

object StaticReference {
  def main(args: Array[String]): Unit = {

    var stackOfIntegers = Stack[Integer]() //Stack of Integer
    var stackOfString = Stack[String]() //Stack of String
    var stack = Stack[Any]() // Empty Stack of any type

  }
}

2. Create a Stack with default values

object StaticReference {
  def main(args: Array[String]): Unit = {

    var stackOfIntegers = Stack[Integer](1, 2, 3, 4, 5) //Stack of Integer
    var stackOfString = Stack[String]("aa", "bb", "cc") //Stack of String
    var stack = Stack[Any](1, "aa", 12.0, 1L, 0.1f) // Stack of any type
  }
}

3. Stack Push operation

object StaticReference {
  def main(args: Array[String]): Unit = {

    var stackOfIntegers = Stack[Integer]() //Stack of Integer
    stackOfIntegers.push(1)
    stackOfIntegers.push(2)
    stackOfIntegers.push(3)

    var stackOfString = Stack[String]() //Stack of String
    stackOfString.push("aa")
    stackOfString.push("bb")

    var stack = Stack[Any]() // Stack of any type
    stack.push(1)
    stack.push("aa")
    stack.push(2L)
  }
}

4. Stack Push multiple elements

object StaticReference {
  def main(args: Array[String]): Unit = {

    var stackOfIntegers = Stack[Integer]() //Stack of Integer
    stackOfIntegers.push(1, 2, 3, 4)

    var stackOfString = Stack[String]() //Stack of String
    stackOfString.push("aa", "bb", "cc")

    var stack = Stack[Any]() // Stack of any type
    stack.push(1, "aa", 2L, 0.01f)
  }
}

5. Stack pop operation

object StaticReference {
  def main(args: Array[String]): Unit = {

    var stackOfIntegers = Stack[Integer](1, 2, 3, 4) //Stack of Integer
    stackOfIntegers.pop

    var stackOfString = Stack[String]("aa", "bb", "cc") //Stack of String
    stackOfString.pop

    var stack = Stack[Any](1, "aa", 2L, 0.01f) // Stack of any type
    stack.pop
  }
}

6. Get the Top element of the stack

object StaticReference {
  def main(args: Array[String]): Unit = {

    var stackOfIntegers = Stack[Integer](1, 2, 3, 4) //Stack of Integer
    println(stackOfIntegers.top)

    var stackOfString = Stack[String]("aa", "bb", "cc") //Stack of String
    println(stackOfString.top)
    
    var stack = Stack[Any](1, "aa", 2L, 0.01f) // Stack of any type
    println(stack.top)
  }
}


Output

1
aa
1

7. Find min/max/distinct in Stack

object StaticReference {
  def main(args: Array[String]): Unit = {

    var stackOfIntegers = Stack[Integer](1, 2, 3, 4, 4, 3) //Stack of Integer
    println(stackOfIntegers.min)
    println(stackOfIntegers.max)
    println(stackOfIntegers.distinct)

  }
}

Output

1
4
Stack(1, 2, 3, 4)

8. Clear Stack

object StaticReference {
  def main(args: Array[String]): Unit = {

    var stackOfIntegers = Stack[Integer](1, 2, 3, 4) //Stack of Integer
    println(stackOfIntegers.clear)
   
    var stackOfString = Stack[String]("aa", "bb", "cc") //Stack of String
    println(stackOfString.clear)
    
    var stack = Stack[Any](1, "aa", 2L, 0.01f) // Stack of any type
    println(stack.clear)

  }
}

Output

()
()
()

9. Find the size of the Stack

bject StaticReference {
  def main(args: Array[String]): Unit = {

    var stackOfIntegers = Stack[Integer](1, 2, 3, 4) //Stack of Integer
    println(stackOfIntegers.size)

    var stackOfString = Stack[String]("aa", "bb", "cc") //Stack of String
    println(stackOfString.size)

    var stack = Stack[Any](1, "aa", 2L, 0.01f) // Stack of any type
    println(stack.size)

  }
}

Output

4
3
4

10. Stack Map operation

object StaticReference {
  def main(args: Array[String]): Unit = {

    var stackOfIntegers = Stack[Integer](1, 2, 3, 4) //Stack of Integer

    stackOfIntegers.map(ele => ele * ele).foreach(println)

  }
}

Output

1
4
9
16

11. Stack Filter operation

object StaticReference {
  def main(args: Array[String]): Unit = {

    var stackOfIntegers = Stack[Integer](1, 2, 3, 4) //Stack of Integer

    stackOfIntegers.map(ele => ele * ele).filter(ele => ele % 2 == 0).foreach(println)

  }
}

Output
4
16

References: Scala Stack official documentation

I hope you like this tutorial and you were able to understand Scala Stack and its various operations.

Thanks for reading and please give us a thumb up and comment below!

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.