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

  • SEARCH
  • Home
  • 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 9230509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:50:35+00:00 2026-06-18T05:50:35+00:00

For example if the map is equal to: z = [‘x’:1, ‘y’:2] For f(2)

  • 0

For example if the map is equal to:

z = ['x':1, 'y':2]

For f(2) I would like it to become:

[x':2, 'y':4]

I know it could be done using a .each loop, question is whether there is a simpler way.

Thank you

  • 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-18T05:50:36+00:00Added an answer on June 18, 2026 at 5:50 am

    There’s the collectEntries function, which is applied to all keys and values in a Map, and returns a new Map with the updated values:

    def z = [x:1, y:2]
    z = z.collectEntries{ key, value -> [(key): value*2] }
    assert z == [x:2, y:4]
    

    A couple of notes here:

    • First, you don’t need to quote string keys in map literals, meaning ['z':1] is the same as [z:1], but the latter is groovier and more legible
    • Second, the return value of the collectEntries closure must be a map, so be careful there
    • Third, when providing dynamic keys in a map, you need to wrap them in parentheses, so you need to use (key), not just key
    • Fourth, you need to assign the result of the method, it doesn’t act in-place.

    However, it’s almost certainly just be cleaner to use each() with the single-argument closure, like so:

    def z = [x:1, y:2]
    z.each{ it.value = it.value*2 }
    assert z == [x:2, y:4]
    

    There’s less gotchyas this way, and it modifies the map in-place, so you don’t have to reassign the variable.

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

Sidebar

Related Questions

Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected
I'd like to copy values that match a predicate (equal ints) from a map<string,int>
In the following example a std::map structure is filled with 26 values from A
Example: http://vincent-massaro.com/map/ Currently, the script allows you to click on a piece of the
I want real example about dynamic site map take it's value from database
On the example http://gmap3.net/issues/contains-failed.html map.getBounds().contains(marker.getPosition()) failed while the marker is visible, if i change
Example: http://83.254.82.145:8080/android/ Look at the menu at the bottom of the map. Try moving
For example, how do I get Output.map from F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map
I want to map, for example, 4 to 'Apr' (3 character abbrev of the
How can I implement STL map sorting by value? For example, I have a

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.