Updated April 13, 2023
Definition of Scala Queue
Queue is a data structure and it follows FIFO. FIFO stands for first in first out. That means the elements insert first in the queue will be removed first. Scala supports two types of queue i.e. mutable and immutable. Immutable queues are those queues which once implemented cannot be changed. whereas mutable queue is different they can be extended further and we can update them as well.
Syntax:
varqueue_name = Queue(values)
In the above syntax, we are assigning value to queue object. See below example to assign value to queue.
varmyQueue = Queue(100,200,400,600,700)
How Queue Works in Scala?
In scala, queue is also part of the collection and it provides us two types of queue i.e. mutable and immutable. But in the scala queue implement in the form of pair of the list. One list added the element into the list and the second list deletes the element from the list. Queue follows the FIFO approach that means first in and first out, the elements we add first to the queue will be removed first from the queue. The queue is also a collection that is used to store the element and retrieve the element.
Scala queue perform two operations;
- Dequeue: Delete an element from the tail of the queue. (at the end)
- Enqueue: Adding an element at the head of the queue. (at the start)
Scala queue extends and implements the following class and interfaces which are mentioned below:
- ArrayDeque
- IndexedSeqOps[A, Queue, Queue[A]]
- StrictOptimizedSeqOps[A, Queue, Queue[A]]
- IterableFactoryDefaults[A, Queue]
- ArrayDequeOps[A, Queue, Queue[A]]
- Cloneable[Queue[A]]
- DefaultSerializable
Now let’s take a look at one simple program for beginners;
import scala.collection.immutable._
object Demo{
def main(args:Array[String]){
var myqueue1 = Queue(100, 200, 700, 100)
var myqueue2:Queue[Int] = Queue(400, 56, 900, 90, 425, 950)
println( "here is the queue one"+myqueue1)
println( "here is the queue second" + myqueue2)
}
}
Here we need to import the package scala.collection.immutable._ to create the queue object. we are creating two queues with the specified value and in the second queue we are also defining the type of value it will take. In the second queue it will take Int only.
Methods of Scala Queue with Examples
Methods and examples of scala queue are given below:
1. ++:()
In method concatenate two queues with all the elements of the second queue will be added at the front of the first queue.
Example
import scala.collection.mutable._
object Main extends App{
// Your code here!
// queue creation
val queue1 = Queue(220, 330, 440, 550)
val queue2 = Queue(40, 60, 70, 30 )
// calling method
valfinalQueue = queue1.++:(queue2)
// output print
print("final queue is ::" + finalQueue)
}
Output:
2. :+()
This method will add one element at the end of the queue and return a new queue.
Example
import scala.collection.mutable._
object Main extends App{
// Your code here!
// queue creation
val queue1 = Queue(220, 330, 440, 550)
valfinalResult = queue1.:+(2)
// output
print("final Result queue is ::" + finalResult)
}
Output:
3.++=()
This method is used to add the element at the end of another queue.
Example
import scala.collection.mutable._
object Main extends App{
// Your code here!
// creating queue
val queue1 = Queue(300, 500, 200, 100)
val queue2 = Queue(340, 20)
valfinalResult = queue2.++=(queue1)
// output
print("final Result queue is ::" + finalResult)
}
Output:
4. map()
This method is used to perform some operation on the existing queue elements one by one and gives us the new queue.
Example
import scala.collection.mutable._
object Main extends App{
// Your code here!
// creating queue
val queue1 = Queue(300, 500, 200, 100)
println("queue before ::" + queue1)
// map method
valfinalResult = queue1.map(x => x + 10)
// Display output
print("finalResult queue after modify elements :: " + finalResult)
}
Output:
5. min()
This method is used to return the minimum value from the queue.
Example
import scala.collection.mutable._
object Main extends App{
// Your code here!
// creating queue
val queue1 = Queue(300, 500, 200, 100)
println("queue before ::" + queue1)
// map method
valfinalResult = queue1.min
// Display output
print("minimum value from the queue is :: " + finalResult)
}
Output:
6. max()
This method is used to return the maximum value from the queue.
Example
import scala.collection.mutable._
object Main extends App{
// Your code here!
// creating queue
val queue1 = Queue(300, 500, 200, 100)
println("queue before ::" + queue1)
// map method
valfinalResult = queue1.max
// Display output
print("maximum value from the queue is :: " + finalResult)
}
Output:
7. sum()
This method is used to sum all the elements present in the queue.
Example
import scala.collection.mutable._
object Main extends App{
// Your code here!
// creating queue
val queue1 = Queue(300, 500, 200, 100)
println("queue before ::" + queue1)
// map method
valfinalResult = queue1.sum
// Display output
print("sum of all values in the queue is :: " + finalResult)
}
Output:
8. last()
This method is used to return the last elements present in the queue.
Example
import scala.collection.mutable._
object Main extends App{
// Your code here!
// creating queue
val queue1 = Queue(300, 500, 200, 100)
println("queue before ::" + queue1)
// map method
valfinalResult = queue1.last
// Display output
print("last value in the queue is :: " + finalResult)
}
Output:
9. contains()
This method is used to check whether the element is present in the queue or not. It will return true or false based on the condition.
Example
import scala.collection.mutable._
object Main extends App{
// Your code here!
// creating queue
val queue1 = Queue(300, 500, 200, 100)
println("queue before ::" + queue1)
// map method
valfinalResult = queue1.contains(200)
// Display output
print("to check 200 present in queue or not ::"+ finalResult)
}
Output:
10. list()
This method is used to return all the elements present in the queue as a list.
Example
import scala.collection.mutable._
object Main extends App{
// Your code here!
// creating queue
val queue1 = Queue(300, 500, 200, 100)
println("queue before ::" + queue1)
// map method
valfinalResult = queue1.toList
// Display output
print("List containing all the queue elements are ::"+ finalResult)
}
Output:
Recommended Articles
We hope that this EDUCBA information on “Scala Queue” was beneficial to you. You can view EDUCBA’s recommended articles for more information.