The size() method is utilized to return the number of elements present in the queue.
Method Definition: def size: Int
Return Type: It returns the number of elements present in the queue.
Example #1:
// Scala program of size() // method // Import Queue import scala.collection.mutable. _
// Creating object object GfG
{ // Main method
def main(args : Array[String])
{
// Creating a queue
val q 1 = Queue( 5 , 2 , 3 , 7 , 1 )
// Print the queue
println(q 1 )
// Applying size method
val result = q 1 .size
// Display output
print( "Number of elements in the queue: " + result)
}
} |
Output:
Queue(5, 2, 3, 7, 1) Number of elements in the queue: 5
Example #2:
// Scala program of size() // method // Import Queue import scala.collection.mutable. _
// Creating object object GfG
{ // Main method
def main(args : Array[String])
{
// Creating a queue
val q 1 = Queue( "a" , "e" , "i" , "o" , "u" )
// Print the queue
println(q 1 )
// Applying size method
val result = q 1 .size
// Display output
print( "Number of elements in the queue: " + result)
}
} |
Output:
Queue(a, e, i, o, u) Number of elements in the queue: 5