Scala Queue sum() method with example
Last Updated :
29 Oct, 2019
The sum() method is utilized to return the sum of all the elements of the queue.
Method Definition: def sum: A
Return Type: It returns the sum of all the elements of the queue.
Example #1:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val q 1 = Queue( 1 , 2 , 3 , 4 , 5 )
println(q 1 )
val result = q 1 .sum
print( "Sum of all elements of the queue: " + result)
}
}
|
Output:
Queue(1, 2, 3, 4, 5)
Sum of all elements of the queue: 15
Example #2:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val q 1 = Queue( 5 , 2 , 13 , 7 , 1 )
println(q 1 )
val result = q 1 .sum
print( "Sum of all elements of the queue: " + result)
}
}
|
Output:
Queue(5, 2, 13, 7, 1)
Sum of all elements of the queue: 28
Share your thoughts in the comments
Please Login to comment...