# Scala sum Map values

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.

Example #1:

 `// Scala program of sum() ` `// method ` ` `  `// Creating object ` `object` `GfG ` `{  ` ` `  `    ``// Main method ` `    ``def` `main(args``:``Array[String]) ` `    ``{ ` `     `  `        ``// Creating a map ` `        ``val` `m``1` `=` `Map(``3` `-``>` `"geeks"``, ``1` `-``>` `"for"``, ``2` `-``>` `"cs"``) ` `         `  `        ``// Applying sum method ` `        ``val` `result ``=` `m``1``.foldLeft(``0``)(``_``+``_``.``_``1``) ` `         `  `        ``// Displays output ` `        ``println(result) ` `     `  `    ``} ` `} `

Output:

```6
```

Here, zero in the foldLeft method is the initial value.
Example #2:

 `// Scala program of sum() ` `// method ` ` `  `// Creating object ` `object` `GfG ` `{  ` ` `  `    ``// Main method ` `    ``def` `main(args``:``Array[String]) ` `    ``{ ` `     `  `        ``// Creating a map ` `        ``val` `m``1` `=` `Map(``3` `-``>` `"geeks"``, ``1` `-``>` `"for"``, ``1` `-``>` `"for"``) ` `         `  `        ``// Applying sum method ` `        ``val` `result ``=` `m``1``.foldLeft(``0``)(``_``+``_``.``_``1``) ` `         `  `        ``// Displays output ` `        ``println(result) ` `     `  `    ``} ` `} `

Output:

```4
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.