In Scala, the sum of the map elements can be done by utilizing foldLeft method.
Syntax : m1.foldLeft(0)(_+_._1)
Here, m1 is used for a Map, foldLeft is a method that takes an initial value zero. it will take previous result and add it to the next map key value.
Return Type: It returns the sum of all the elements of the map.
Here, zero in the foldLeft method is the initial value.
- How to get all the values from a Scala map
- Scala Product Map Values
- How to Reverse keys and values in Scala Map
- Scala Tutorial – Learn Scala with Step By Step Guide
- Set in Scala | Set-1
- Set in Scala | Set-2
- Scala Map
- Scala | Either
- Scala Int abs() method with example
- Scala Map take() method with example
- Scala Int -(x: Int) method with example
- Scala Int until(end: Int) method with example
- Scala map contains() method with example
- Scala For Comprehensions
- Scala short /(x: Int): Int
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.