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

By Nadim Bahadoor | Last updated: July 25, 2017 at 14:37 pm

Overview

In this tutorial, we will learn how to use Scala's Immutable Queue and perform common operations such as initialization, adding or removing elements, find elements by key within the Queue, enqueue and dequeue.

 

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

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.immutable.Queue
println("Step 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)

 

NOTE:

2. How to access elements at specific index in a Queue

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


println("\nStep 2: How to access elements at specific index in a Queue")
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 at specific index in a Queue
Element at index 0 = Plain Donut
Element at index 0 = Strawberry Donut
Element at index 0 = Chocolate Donut

 

3. How to add elements in a Queue using :+

The code below shows how to add elements in a Queue using :+.


println("\nStep 3: How to add elements in a Queue using :+")
val queue2: Queue[String] = queue1 :+ "Glazed Donut"
println(s"Elements of queue2 = $queue2")

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


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

 

4. How to add elements in Queue using enqueue function

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


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

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


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

 

5. How to take the first element from the Queue using dequeue function

The code below shows how to take the first element from the Queue using dequeue function.


println("\nStep 5: How to take the first element from the Queue using dequeue function")
val dequeue: (String, Queue[String]) = queue1.dequeue
println(s"First element dequeue = ${dequeue._1}")
println(s"Remaining elements after dequeue = ${dequeue._2}")

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


Step 5: How to take the first element from the Queue using dequeue function
First element dequeue = Plain Donut
Remaining elements after dequeue = Queue(Strawberry Donut, Chocolate Donut)

 

NOTE:

  • Calling dequeue returns a Tuple2 with the last element that was inserted to the Queue and the remaining elements of the queue.

6. How to add two Queues together using ++

The code below shows how to add two Queues together using ++


println("\nStep 6: How to add two Queues together using ++")
val queue3: Queue[String] = queue1 ++ Queue[String]("Glazed Donut", "Vanilla Donut")
println(s"Elements in queue3 = $queue3")

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


Step 6: How to add two Queues together using ++
Elements in queue3 = Queue(Plain Donut, Strawberry Donut, Chocolate Donut, Glazed Donut, Vanilla Donut)

 

7. How to initialize an empty Queue

The code below shows how to initialize an empty Queue.


println("\nStep 7: 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 7: How to initialize an empty Queue
Empty Queue = Queue()

 

This concludes our tutorial on Learn How To Use Scala's Immutable 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 with 3 elements
  • How to access elements at specific index in a Queue
  • How to add elements in a Queue using :+
  • How to add elements in Queue using enqueue function
  • How to take the first element from the Queue using dequeue function
  • How to add two Queues together using ++
  • 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 Immutable Sequence.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Senior Software Developer | Elsevier
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on