
- Scala Collections - Home
- Scala Collections - Overview
- Scala Collections - Environment Setup
- Scala Collections - Arrays
- Scala Collections - Array
- Scala Collections - Multi-Dimensional Array
- Scala Collections - Array using Range
- Scala Collections - ArrayBuffer
- Scala Collections - Lists
- Scala Collections - List
- Scala Collections - ListBuffer
- Scala Collections - ListSet
- Scala Collections - Vector
- Scala Collections - Sets
- Scala Collections - Set
- Scala Collections - BitSet
- Scala Collections - HashSet
- Scala Collections - TreeSet
- Scala Collections - Maps
- Scala Collections - Map
- Scala Collections - HashMap
- Scala Collections - ListMap
- Scala Collections - Miscellaneous
- Scala Collections - Iterator
- Scala Collections - Option
- Scala Collections - Queue
- Scala Collections - Tuple
- Scala Collections - Seq
- Scala Collections - Stack
- Scala Collections - Stream
- Scala Collections Combinator methods
- Scala Collections - drop
- Scala Collections - dropWhile
- Scala Collections - filter
- Scala Collections - find
- Scala Collections - flatMap
- Scala Collections - flatten
- Scala Collections - fold
- Scala Collections - foldLeft
- Scala Collections - foldRight
- Scala Collections - map
- Scala Collections - partition
- Scala Collections - reduce
- Scala Collections - scan
- Scala Collections - zip
- Scala Collections Useful Resources
- Scala Collections - Quick Guide
- Scala Collections - Useful Resources
- Scala Collections - Discussion
Scala Collections - Queue
Queue is First In First Out, FIFO data structure and allows to insert and retrieve elements in FIFO manner.
Declaring Queue Variables
The following is the syntax for declaring an Queue variable.
Syntax
val queue = Queue(1, 2, 3, 4, 5)
Here, queue is declared as an Queue of numbers. Value can be added at front by using commands like the following −
Command
queue.enqueue(6)
Value can be retrived at front by using commands like the following −
Command
queue.dequeue()
Processing Queue
Below is an example program of showing how to create, initialize and process Queue −
Example
import scala.collection.mutable.Queue object Demo { def main(args: Array[String]) = { var queue = Queue(1, 2, 3, 4, 5); // Print queue elements queue.foreach{(element:Int) => print(element + " ")} println(); // Print first element println("First Element: " + queue.front) // Add an element queue.enqueue(6); // Print queue elements queue.foreach{(element:Int) => print(element+ " ")} println(); // Remove an element var dq = queue.dequeue; // Print dequeued element println("Dequeued Element: " + dq) // Print queue elements queue.foreach{(element:Int) => print(element+ " ")} } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
\>scalac Demo.scala \>scala Demo
Output
1 2 3 4 5 First Element: 1 1 2 3 4 5 6 Dequeued Element: 1 2 3 4 5 6
Checking if the Queue is Empty
To check if the Queue is empty, you can use the isEmpty method. This method returns true if the Queue is empty, otherwise false. This is useful for ensuring that the Queue has elements before performing operations that assume it is not empty.
Syntax
val isEmpty = queue.isEmpty
Example
import scala.collection.mutable.Queue object Demo { def main(args: Array[String]): Unit = { val queue = Queue[Int]() println(s"Is queue empty? ${queue.isEmpty}") // Enqueue an element into the queue queue.enqueue(1) println(s"Is queue empty? ${queue.isEmpty}") } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
Is queue empty? true Is queue empty? false
Iterating through a Queue
You can iterate through the elements of a Queue using a foreach loop. This allows you to perform an action on each element in the Queue, which can be useful for tasks like printing elements or applying a function to each one.
Example
Try following example for iterating through a queue -
import scala.collection.mutable.Queue object Demo { def main(args: Array[String]): Unit = { val queue = Queue(1, 2, 3, 4, 5) // Iterate and print each element in the queue queue.foreach(element => println(element)) } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
1 2 3 4 5
Transforming Queues
You can transform a Queue by applying a transformation function to each element using the map method. This method returns a new Queue with the transformed elements, allowing for operations like scaling, formatting, or any other transformation.
Example
Try following example for doubling elements of queue -
import scala.collection.mutable.Queue object Demo { def main(args: Array[String]): Unit = { val queue = Queue(1, 2, 3, 4, 5) // Transform each element in the queue by doubling it val doubledQueue = queue.map(_ * 2) println(doubledQueue) } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
Queue(2, 4, 6, 8, 10)
Filtering Queues
You can filter elements in a Queue based on a predicate function using the filter method. This method returns a new Queue containing only the elements that satisfy the predicate, which is useful for extracting subsets of the data.
Example
Try following example for filtering even numbers from given queue -
import scala.collection.mutable.Queue object Demo { def main(args: Array[String]): Unit = { val queue = Queue(1, 2, 3, 4, 5) // Filter and keep only even elements in the queue val evenQueue = queue.filter(_ % 2 == 0) println(evenQueue) } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
Queue(2, 4)
Converting a Queue to Other Collections
You can convert a Queue to other collections such as lists, arrays, and sequences. This is useful when you need to use the Queue data in a different context where a different type of collection is required.
Example
Try following example for converting a queue to other collections -
import scala.collection.mutable.Queue object Demo { def main(args: Array[String]): Unit = { val queue = Queue(1, 2, 3, 4, 5) // Convert queue to list val list = queue.toList // Convert queue to array val array = queue.toArray // Convert queue to sequence val seq = queue.toSeq println(list) println(array.mkString(", ")) println(seq) } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
List(1, 2, 3, 4, 5) 1, 2, 3, 4, 5 Queue(1, 2, 3, 4, 5)
Scala Queue Summary
- Queues are mutable collections that follow the FIFO principle.
- You can perform basic operations such as enqueuing, dequeuing, and checking the front element on a Queue.
- Queues can be iterated, transformed, filtered, and converted to other collections.
- Scala provides the Queue class in the collection.mutable package for creating and manipulating queues.