Introduction to Scala Queue with Examples

If You are interested to learn about the about the Scala Pattern Matching

Queue implements a data structure that allows inserting and retrieving elements in a first-in-first-out (FIFO) manner.

In scala, Queue is implemented as a pair of lists. One is used to insert the elements and second to contain deleted elements. Elements are added to the first list and removed from the second list.


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:

  • Array Deque
  • Indexed SeqOps[A, Queue, Queue[A]]
  • Strict Optimized SeqOps[A, Queue, Queue[A]]
  • Iterable Factory Defaults[A, Queue]
  • Array Deque Ops[A, Queue, Queue[A]]
  • Cloneable [Queue[A]]
  • Default Serializable

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:

Scala Queue-1.1

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:

Scala Queue-1.2

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:

Scala Queue-1.3

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:

Scala Queue-1.4

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:

Scala Queue-1.5

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:

Scala Queue-1.6

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:

Output-1.7

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:

Output-1.8

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:

Output-1.9

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:

Output-1.10
Introduction to Scala Queue with Examples
Show Buttons
Hide Buttons