Scala Tutorial - Learn How To Use Scala's Mutable Queue

By Nadim Bahadoor | Last updated: February 3, 2020 at 7:31 am

Overview

In this tutorial, we will learn how to use Scala's Mutable Queue to perform common operations such as initialize a Queue, access elements at specific index, enqueue and dequeue elements and create an empty Queue.

 

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 Mutable Queue will be discussed in Chapter 8 on Collection Functions.

 

What is a Queue?

As per Wikipedia, a Queue is a data structure which follows the FIFO (First In First Out) semantics. In other words, the first element that was added to the queue will be the first one to be removed.

 

As per the Scala Documentation, a mutable Queue has an internal data structure which allows you to insert and retrieve elements in a FIFO manner.

 

Steps

1. How to initialize a Queue with 3 elements

The code below shows how to initialize a Queue with 3 elements.


import scala.collection.mutable.Queue
println("\nStep 1: How to initialize a Queue with 3 elements")
val queue1: Queue[String] = Queue("Plain Donut", "Strawberry Donut", "Chocolate Donut")
println(s"Elements of queue1 = $queue1")

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

Step 1: How to initialize a Queue with 3 elements
Elements of queue1 = Queue(Plain Donut, Strawberry Donut, Chocolate Donut)

 

2. How to access elements of Queue at specific index

The code below shows how to access elements of Queue at specific index.


println("\nStep 2: How to access elements of Queue at specific index")
println(s"Element at index 0 = ${queue1(0)}")
println(s"Element at index 0 = ${queue1(1)}")
println(s"Element at index 0 = ${queue1(2)}")

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

Step 2: How to access elements of Queue at specific index
Element at index 0 = Plain Donut
Element at index 0 = Strawberry Donut
Element at index 0 = Chocolate Donut

3. How to add elements to Queue using +=

The code below shows how to add elements to Queue using +=.


println("\nStep 3: How to add elements to Queue using +=")
queue1 += "Glazed Donut"
println(s"Elements of queue1 = $queue1")

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


Step 3: How to add elements to Queue using +=
Elements of queue1 = Queue(Plain Donut, Strawberry Donut, Chocolate Donut, Glazed Donut)

 

4. How to add elements to Queue using enqueue

The code below shows how to add elements to Queue using enqueue.


println("\nStep 4: How to add elements to Queue using enqueue")
queue1.enqueue("Vanilla Donut")
println(s"Enqueue element Vanilla Donut onto queue1 = $queue1")

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

Step 4: How to add elements to Queue using enqueue
Enqueue element Vanilla Donut onto queue1 = Queue(Plain Donut, Strawberry Donut, Chocolate Donut, Glazed Donut, Vanilla Donut)

 

5. How to take the first element or head from the Queue

The code below shows how to take the first element or head from the Queue.

println("\nStep 5: How to take the first element or head from the Queue")
val dequeuedElement: String = queue1.dequeue
println(s"Dequeued element = $dequeuedElement")
println(s"Elements of queue1 after dequeue = $queue1")

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

Step 5: How to take the first element or head from the Queue
Dequeued element = Plain Donut
Elements of queue1 after dequeue = Queue(Strawberry Donut, Chocolate Donut, Glazed Donut, Vanilla Donut)

 

6. How to initialize an empty Queue

The code below shows how to initialize an empty Queue.


println("\nStep 6: How to initialize an empty Queue")
val emptyQueue: Queue[String] = Queue.empty[String]
println(s"Empty Queue = $emptyQueue")

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


Step 6: How to initialize an empty Queue
Empty Queue = Queue()

 

This concludes our tutorial on Learn How To Use Scala's Mutable Queue 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 Queue
  • How to access elements of Queue at specific index
  • How to add elements to Queue using +=
  • How to add elements to Queue using enqueue
  • How to take the first element or head from the Queue
  • How to initialize an empty Queue

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 Mutable PriorityQueue.

 

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: