I have the following map:
def map = [];
map.add([ item: "Shampoo", count: 5 ])
map.add([ item: "Soap", count: 3 ])
I would like to get the sum of all the count properties in the map. In C# using LINQ, it would be something like:
map.Sum(x => x.count)
How do I do the same in Groovy?
Assuming you have a list like so:
Then there are multiple ways of doing it. The most readable is probably
Or you could use the Closure form of sum on the whole list
Or you could even use a more complex route such as inject
All of these give the same result.
I would use the first one.