Scala Tutorial - Learn How To Use Scala's Immutable Stack

By Nadim Bahadoor | Last updated: February 6, 2020 at 12:36 pm

Overview

In this tutorial, we will learn how to use Scala's Immutable Stack to perform common operations such as push new elements to the top of the Stack and remove or pop elements from the Stack. This tutorial is best suited for Scala versions prior to Scala 2.13.0. It is worth noting that as of Scala 2.13.0, the Immutable Stack class has been deprecated, and you can use our tutorial for using an Immutable List to achieve stack-like operations.

 

And, don't forget to review the Data Structures tutorial before delving into Scala's Immutable and Mutable collections.

 

More advanced functions such as aggregate, fold, reduce, map, flatMap etc on the Immutable Stack will be discussed in Chapter 8 on Collection Functions.

 

 

What is a Stack?

As per Wikipedia, a Stack is a data structure which follows the LIFO (Last In First Out) semantics.

 

It typically provides a push() method to add element at the top of the Stack and a pop() method to take the most recently added element from the top of the Stack.

Steps

1. How to initialize Stack with 3 elements

The code below shows how to initialize Stack with 3 elements.


import scala.collection.immutable.Stack
println("Step 1: How to initialize Stack with 3 elements")
val stack1: Stack[String] = Stack("Plain Donut", "Strawberry Donut", "Chocolate Donut")
println(s"Elements of stack1 = $stack1")

You should see the following output when you run your Scala application in IntelliJ:


Step 1: How to initialize Stack with 3 elements
Elements of stack1 = Stack(Plain Donut, Strawberry Donut, Chocolate Donut)

NOTE:

  • Immutable Stack is deprecated...  so do not use it!
  • Instead let's use an Immutable List as per the Scala API documentation to achieve the same Stack behaviour.

2. How to initialize a Stack using an Immutable List

The code below shows how to initialize a Stack using an Immutable List.


println("\nStep 2: How to initialize a Stack using an Immutable List")
val stack2: List[String] = List("Plain Donut","Strawberry Donut","Chocolate Donut")
println(s"Using an Immutable List for stack, elements are = $stack2")

You should see the following output when you run your Scala application in IntelliJ:


Step 2: How to initialize a Stack using an Immutable List
Using an Immutable List for stack, elements are = List(Plain Donut, Strawberry Donut, Chocolate Donut)

NOTE:

3. Push one element at the top of the stack using :: of Immutable List

The code below shows push one element at the top of the stack using :: of Immutable List.


println("\nStep 3: Push one element at the top of the stack using :: of Immutable List")
val stack3: List[String] = "Vanilla Donut" :: stack2
println(s"Using an Immutable List for stack, elements after push = $stack3")

You should see the following output when you run your Scala application in IntelliJ:


Step 3: Push one element at the top of the stack using :: of Immutable List
Using an Immutable List for stack, elements after push = List(Vanilla Donut, Plain Donut, Strawberry Donut, Chocolate Donut)

NOTE:

  • Element Vanilla Donut is at the top of the Stack

4. Push N elements at the top of the stack using :: of Immutable List

The code below shows push N elements at the top of the stack using :: of Immutable List.


println("\nStep 4: Push N elements at the top of the stack using :: of Immutable List")
val stack4: List[String] = "Glazed Donut" :: "Vanilla Donut" :: stack2
println(s"Using an Immutable List for stack, elements after pushing N elements = $stack4")

You should see the following output when you run your Scala application in IntelliJ:

Step 4: Push N elements at the top of the stack using :: of Immutable List
Using an Immutable List for stack, elements after pushing N elements = List(Glazed Donut, Vanilla Donut, Plain Donut, Strawberry Donut, Chocolate Donut)

 

5. Pop element from the Stack using tail function of Immutable List

The code below shows pop element from the Stack using tail function of Immutable List.


println("\nStep 5: Pop element from the Stack using tail function of Immutable List")
val stack5: List[String] = stack2.tail
println(s"Using an Immutable List for stack, elements after tail function to simulate Stack pop = $stack5")

You should see the following output when you run your Scala application in IntelliJ:


Step 5: Pop element from the Stack using tail function of Immutable List
Using an Immutable List for stack, elements after tail function to simulate Stack pop = List(Strawberry Donut, Chocolate Donut)

 

6. How to initialize an empty Stack using Immutable List

The code below shows how to initialize an empty Stack using Immutable List.


println("\nStep 6: How to initialize an empty Stack using Immutable List")
val emptyStack: List[String] = List.empty[String]
println(s"Using an Immutable List for stack, the empty stack = $emptyStack")

You should see the following output when you run your Scala application in IntelliJ:


Step 6: How to initialize an empty Stack using Immutable List
Using an Immutable List for stack, the empty stack = List()

This concludes our tutorial on Learn How To Use Scala's Immutable Stack and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

Summary

In this tutorial, we went over the following:

  • How to initialize Stack with 3 elements
  • How to initialize a Stack using an Immutable List
  • Push one element at the top of the stack using :: of Immutable List
  • Push N elements at the top of the stack using :: of Immutable List
  • Pop element from the Stack using tail function of Immutable List
  • How to initialize an empty Stack using Immutable List

Tip

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will show you how to use Scala's Immutable Stream.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Technology and Finance Consultant with over 14 years of hands-on experience building large scale systems in the Financial (Electronic Trading Platforms), Risk, Insurance and Life Science sectors. I am self-driven and passionate about Finance, Distributed Systems, Functional Programming, Big Data, Semantic Data (Graph) and Machine Learning.
Other allaboutscala.com tutorials you may like: