Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

scala
communitycreator

What is Queue.size() 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.

The Queue.size() function in Scala is used to get the current size of a queue. It returns the number of elements in the queue.

The illustration below shows the visual representation of the Queue.size() function.

Visual representation of the Queue.size function

To use the Queue.size() function, you must include the following module.

  scala.collection.mutable.Queue

Syntax


queue_name.size;
// where the queue_name is the name of the queue object

Parameter

The Queue.size() function does not require any parameters.

Return value

This function returns the number of elements in the Queue object.

Code

The following code shows how to use the Queue.size() 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: " + queue);
println("Following is the size of Queue: " + queue.size);
}

Explanation

The enqueue() method is first used to add values to the object queue. The size() method in line 1212 returns the number of elements in the object 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.

Keep Exploring