Update map using findAll and each in groovy

groovy

Solution

The root cause is `findAll` returns a new Map instance.

So you could try:

newMap = m.findAll { it.value > 1}.each { 
    it.value = 4
}
println m      //No change
println newMap //This is what you need!

output is

[a:1, b:2, d:3] 
[b:4, d:4]

Problem

I would like to update values in map in Groovy filling certain criteria. Here is my code: ``` def m = [:] m['a'] = 1 m['b'] = 2 m['d'] = 3 m.findAll { it.value > 1}.each { it.value = 4 } println m ``` But the result is following: ``` [a:1, b:2, d:3] ``` Is there any way to do it using both findAll and each? Or I must use ``` m.each {if (it.value>1) it.value=4} ```

Original source