Adrian Adendrata Adrian Adendrata - 3 months ago 18
Groovy Question

for each loop in groovy

I want to ask, how implement for each in Groovy. I'm new in Groovy.
i have example code in java, but i don't implement these code in Groovy

Java:

for (Object objKey : tmpHM.keySet()) {
HashMap objHM = (HashMap) list.get(objKey);
}


this morning i have to read http://groovy.codehaus.org/Looping and try to translate my Java code to Groovy, but it's not working.

for (objKey in tmpHM.keySet()) {
HashMap objHM = (HashMap) list.get(objKey);
}


thank you... :))

Answer

as simple as:

tmpHM.each{ key, value -> 
  doSomethingWithKeyAndValue()
}