Scala Queue operations explained

39 total views, 1 views today

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

Queue is one of the basic and important data structures of any programming language. Scala does support Queue.

Queue is part of the Scala collection package and it is based on IndexedSeq.

What is a Scala queue?

Scala queue(or basically queue) is based on the FIFO pattern. i.e., First-In-First-Out. Queue allows us to insert and remove elements as per the FIFO pattern.

The two main operation of a Scala queue is,

  1. enqueue
  2. dequeue

 

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

For this tutorial, we concentrate only on the mutable queue. By default, in scala, immutable collections will be imported. To import or use any mutable collection, you have to explicitly import the below package

import scala.collection.mutable.Queue

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 queue.

1. Create an empty Scala queue

var name: Queue[String] = Queue() //Queue of String
var age: Queue[Int] = Queue() //Queue of Integer

2. Create a queue with default values

var name: Queue[String] = Queue("a","b","c") //Queue of String
var age: Queue[Int] = Queue(1,2,3,4) //Queue of Integer

3. Enqueue/Insert an element in the queue

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

    var name: Queue[String] = Queue("a", "b", "c") //Queue of String
    var age: Queue[Int] = Queue(1, 2, 3, 4) //Queue of Integer

    name.enqueue("d")
    name.enqueue("e")

    age.enqueue(5)
    age.enqueue(6)

    name.foreach(print _)
  println() // line break
    age.foreach(print _)

}

Output

abcde
123456

4. Dequeue/remove an element from the queue

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

    var name: Queue[String] = Queue("a", "b", "c") //Queue of String
    var age: Queue[Int] = Queue(1, 2, 3, 4) //Queue of Integer

    name.dequeue()
    name.dequeue()

    age.dequeue()
    age.dequeue()

    name.foreach(print _)
    println()
    age.foreach(print _)

}

Output

c
34

5. Get head/first element from the queue

var name: Queue[String] = Queue("a", "b", "c") //Queue of String
var age: Queue[Int] = Queue(1, 2, 3, 4) //Queue of Integer

println(name.head)
println(age.head)

Output

a
1

6. Get tail/rear element from the queue

var name: Queue[String] = Queue("a", "b", "c") //Queue of String
var age: Queue[Int] = Queue(1, 2, 3, 4) //Queue of Integer

println(name.tail)
println(age.tail)

Output

Queue(b, c)
Queue(2, 3, 4)

7. Add elements to the end of the queue(Append and AppendAll)

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

    var name: Queue[String] = Queue("a", "b", "c") //Queue of String

    name.+=("d")
    name.+=("e") //Append single element
    name.+=("f", "g") //Append multiple elements
    name.++=(Queue("h", "i", "j")) //Append a collection

    name.foreach(println _)
}

Output

a
b
c
d
e
f
g
h
i
j

8. Prepend elements in front of the queue

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

    var name: Queue[String] = Queue("a", "b", "c") //Queue of String

    name.+=:("d")
    name.+=:("e")
    name.+=:("f")

    name.foreach(println _)

}

Output

f
e
d
a
b
c

9. Find min,max,sum and distinct in Scala queue

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

    var name: Queue[Int] = Queue(2, 3, 4, 5, 6, 7, 2) 

    println(name.max) //max
    println(name.min) //min
    println(name.sum) //sum
    println(name.distinct) //distinct

}

Output

7
2
29
Queue(2, 3, 4, 5, 6, 7)

10. Map operation

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

    var name: Queue[Int] = Queue(2, 3, 4, 5, 6, 7, 2)

    name.map(ele => ele * ele).foreach(println _)

}

Output

4
9
16
25
36
49
4

11. Filter operation

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

    var name: Queue[Int] = Queue(2, 3, 4, 5, 6, 7, 2)

  //Filter even numbers
    name.filter(ele => ele % 2 == 0).foreach(println _)

}

Output

2
4
6
2

References: Scala Queue official documentation

I hope you like this tutorial and you were able to understand Scala Queue 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.