What is queue.front in Scala?

The queue.front function in Scala is used to get the reference of the element at the front of the queue.

Figure 1 shows the visual representation of the queue.front function.

Figure 1: Visual representation of the queue.front function

The following module is required in order to use the function scala.collection.mutable.Queue.

Syntax


queue_name.front;
// where the queue_name is the name of the queue

Parameter

This function does not require a parameter.

Return value

queue.front is used to get the reference of the element at the queue’s front.

  • It does not remove that element from the queue.
  • It throws an error if there are no elements in the queue.

Code

The following code shows how to use the queue.front function.

import scala.collection.mutable.Queue
object Main extends App {
var queue = Queue[Int]()
queue.enqueue(1);
queue.enqueue(3);
queue.enqueue(5);
queue.enqueue(2);
queue.enqueue(0);
//Queue = 1->3->5->2->0
println("Following are the elements in Queue before using queue.front: " + queue);
println("The element in the front of Queue: "+queue.front);
println("Following are the elements in Queue after using queue.front: " + queue);
}