Scala Stack sum() method with example Last Updated : 03 Nov, 2019 Comments Improve Suggest changes Like Article Like Report In Scala Stack class, the sum() method is utilized to return the sum of all the elements of the stack. Method Definition: def sum: A Return Type: It returns the sum of all the elements of the stack. Example #1: Scala // Scala program of sum() // method // Import Stack import scala.collection.mutable._ // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating stack val s1 = Stack(1, 2, 3, 4, 5) // Print the stack println(s1) // Applying sum method val result = s1.sum // Display output print("Sum of all elements: " + result) } } Output: Stack(1, 2, 3, 4, 5) Sum of all elements: 15 Example #2: Scala // Scala program of sum() // method // Import Stack import scala.collection.mutable._ // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating stack val s1 = Stack(5, 2, 13, 7, 1) // Print the stack println(s1) // Applying sum method val result = s1.sum // Display output print("Sum of all elements: " + result) } } Output: Stack(5, 2, 13, 7, 1) Sum of all elements: 28 Comment More infoAdvertise with us Next Article Scala Stack sum() method with example R rupesh_rao Follow Improve Article Tags : Scala Scala Scala-Method scala-collection Similar Reads Scala Set sum() method with example The sum() method is utilized to find the sum of all the elements of the set. Method Definition: def sum: A Return Type: It returns the sum of all the elements of the set. Example #1: Scala // Scala program of sum() // method // Creating object object GfG { // Main method def main(args:Array[String]) 1 min read Scala Stack :+() method with example In Scala, scala.collection.mutable implements Stack data structure. The :+ method is used to create a copy of this stack with an element appended. Method Definition - def :+(elem: A) Returns - a new stack consisting of all elements of this stack followed by elem. Example #1: SCALA // Scala program o 1 min read Scala Stack map() method with example In Scala Stack class, the map() method is utilized to build a new stack by applying a function to all elements of the given stack. Method Definition: def map[B](f: (A) => B): Stack[B] Return Type: It returns a new stack containing all the elements after applying the given function. Example #1: Sc 2 min read Scala Set ++() method with example The ++() method is utilized to add elements of one set to another set. Method Definition: def ++(elems: A): Set[A] Return Type: It returns a new set containing elements of both the sets. Example #1: Scala // Scala program of ++() // method // Creating object object GfG { // Main method def main(args 1 min read Scala Set +() method with example The +() method is utilized to create a new set with an additional element unless the element is already present. Method Definition: def +(elem: A): Set[A] Return Type: It returns a new set with an additional element unless the element is already present. Example #1: Scala // Scala program of +() // 1 min read Like