What is queue.clear in Scala?

The queue.clear function in Scala is used to remove all the elements from a queue.

The illustration below shows a visual representation of the queue.clear function.

Figure 1: Visual representation of queue.clear function

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

Syntax

queue_name.clear

queue_name is the name of the queue.

Parameter

The queue.clear function does not require any parameters.

Return value

Scala’s queue.clear function does not return anything.

Code

The following code shows how to use queue.clear function in Scala.

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 clear: " + queue);
queue.clear;
println("Following are the elements in Queue after clear: " + queue);
}

Free Resources