Scala Tutorial - Learn How To Use Scala's Immutable List To Simulate A Stack For Scala Version 2.13.0 And Above

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

Overview

In this section, we will learn how to use Scala’s Immutable Stack and perform common operations, such as, initialization, pushing new elements to the top of the Stack, and removing or popping elements from the Stack. It is important to note that as of Scala version 2.13.0, the Stack class has been deprecated in favor of using an Immutable List to achieve stack-like push and pop operations, as expected from a Stack data structure. As a reminder, a Stack data structure follows the LIFO (Last In First Out) semantics. Typically a push() operation adds element at the top of the Stack, and a pop() operation removes the most recently added element from the top of the Stack.

 

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.

Steps

1. How to initialize a Stack using an Immutable List

Let’s start off by initializing an Immutable List with three elements of type String to represent our Stack data type.


println("\nStep1:How to initialize a Stack using an ImmutableList")
val stack1 = List("PlainDonut","StrawberryDonut","ChocolateDonut")
println(s"Using an Immutable List for stack, elements are = $stack1") 

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


Step 1: 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:

  • 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.
  • Refer to the tutorial on How To Create And Use Immutable List for examples of common operations such as add or remove elements.

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

A common behavior from a Stack data type is to push an element at the top of the Stack. Since we are using a List as our Stack, you can use the :: notation to achieve the push operation. The code below adds the Vanilla Donut element at the top of the Stack.


println("\nStep2: Push one element at the top of the stack using :: of Immutable List") 
val stack2 = "VanillaDonut" :: stack1 
println(s"Using an Immutable List for stack, elements after push = $stack2") 

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


Step 2: 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

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

Similar to Step 2, you can use the :: notation to push multiple elements at the top of the Stack.


println("\nStep3: Push N elements at the top of the stack using :: of Immutable List") 
val stack3 = "GlazedDonut" :: "VanillaDonut" :: stack1 
println(s"Using an Immutable List for stack, elements after pushing N elements = $stack3") 

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

Step 3: 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) 

 

4. Pop element from the Stack using tail method of Immutable List

The second expected operation for a given Stack data structure is the pop operation, or removing, the top-most element. The tail method from the Immutable List can be used to simulate the pop operation of a Stack and, in the code below, the Plain Donut element is popped.


println("\nStep4: Pop element from the Stack using tail method of ImmutableList") 
val stack4 = stack1.tail 
println(s"Using an Immutable List for stack, elements after tail method to simulate Stack pop = $stack4") 

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


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

 

5. Capture the popped element

As part of the pop operation, what if you also needed to capture the popped element? The :: notation comes in very handy once again as shown below.


val popped :: stack = stack1 
println(s"Popped element = $popped, the stack after the pop operation = $stack") 

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


Step 5: Capture the popped element
Popped element = Plain Donut, the stack after the pop operation = List(Strawberry Donut, Chocolate Donut) 

 

6. How to initialize an empty Stack using Immutable List

To initialize an empty Stack, you make use of the convenient empty() method from the Immutable List. In the code below, we create an empty List of type String to represent an empty Stack data structure.


println("\nStep6: How to initialize an empty Stack using Immutable List") 
val emptyStack = 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 List To Simulate A Stack For Scala Version 2.13.0 And Above 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 a Stack using an Immutable List
  • Push one element at the top of the stack using :: of the Immutable List
  • Push N elements at the top of the stack using :: of the Immutable List
  • Pop element from the Stack using the tail method of the Immutable List
  • Capture the popped element
  • How to initialize an empty Stack using Immutable List

Tip

  • Don't forget to review Chapter 8 on Collection Functions where we will go over the rich suite of functions exposed on collections such as aggregate, collect, fold, reduce, map, flatMap, scan, partition etc
  • The Scala documentation provides additional details on using an Immutable List for stack-like operations.

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: