Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8385235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:37:06+00:00 2026-06-09T17:37:06+00:00

I have the following map: def map = []; map.add([ item: Shampoo, count: 5

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:37:07+00:00Added an answer on June 9, 2026 at 5:37 pm

    Assuming you have a list like so:

    List list = [ [item: "foo", count: 5],
                  [item: "bar", count: 3] ]
    

    Then there are multiple ways of doing it. The most readable is probably

    int a = list.count.sum()
    

    Or you could use the Closure form of sum on the whole list

    int b = list.sum { it.count }
    

    Or you could even use a more complex route such as inject

    int c = list.count.inject { tot, ele -> tot + ele } // Groovy 2.0
    //  c = list.count.inject( 0 ) { tot, ele -> tot + ele } // Groovy < 2.0
    

    All of these give the same result.

    assert ( a == b ) && ( b == c ) && ( c == 8 )
    

    I would use the first one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a map in groovy that looks like the following... def book =
I have the following code: def maturities InfoItem.find_all_by_work_order(self.work_order).map(&:maturity) end I was thinking about changing
I have the following map extension object ImplicitMap { implicit def extendMap(m : Map[String,Any])
I have the following map which I want to iterate: (def db {:classname com.mysql.jdbc.Driver
Is filter/map equivalent to list comprehension? Suppose I have the following function def fib_gen():
I have the following function: def heading_positions(self): return map( lambda h: {'{t}.{c}'.format(t=h.table_name,c=h.column_name) : h.position
I have the following input: (def nums [123456789012 123456789012]) I'd like the following output:
I have the following code: Map<String, ObjectType> objectMap = new HashMap<String, ObjectType>(); for (ObjectType
I have the following code private Map<KEY, Object> values = new HashMap<KEY, Object>(); public
suppose i have the following: boost::unordered_map< string , someValueType > map; someValueType& value =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.