Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

scala
communitycreator

What is queue.clear in Scala?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

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);
}

RELATED TAGS

scala
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring