I have an Array List in groovy in below format. I want the sum of integer values in this list.
[ {"value":1}, {"value":1}, {"value":10}, {"value":11}, {"value":12}]
Expected Output
1+1+10+11+12=35
Oh it's very easy.
list.value.sum()
I would prefer using the Groovy Spread Operator.
The Spread Operator (*.) is used to invoke an action on all items of an aggregate object.
Specific to your question, the best way coding the desired result is:
list*.value.sum()
The difference is only a * but it is best practice to use the language correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With