I have simple hashmap and want to manipulate it but IDE does not recognize the methods. Completely new to Groovy so not sure what is wrong.
def percentages = [
"x1" : "20%",
"x2" : "30%",
"x3" : "0.4",
"x4" : "50%",
"x5" : "0.6"]
percentages.each {
val ->
if(val[2].endsWith("%")) {
val[2].deleteCharAt(val.length()-1)
println val
}
}
When you do percentages.each
you iterate over map and each element is java.util.Map.Entry
. Here is probably what you need:
import java.util.Map.Entry
def percentages = [
"x1": "20%",
"x2": "30%",
"x3": "0.4",
"x4": "50%",
"x5": "0.6"]
percentages.each { Entry<String, String> val ->
if (val.value.endsWith("%")) {
val.value = val.value.substring(0, val.value.length() - 1)
println val
}
}
or even more compact, as @daggett suggested:
def percentages = [
"x1": "20%",
"x2": "30%",
"x3": "0.4",
"x4": "50%",
"x5": "0.6"]
percentages.each {k, v ->
if (v.endsWith("%")) {
percentages[k] = v.substring(0, v.length() - 1)
println percentages[k]
}
}