groovy – Collection Operators – Iterate over a collection

Lists

def lst = ['foo', 'bar', 'baz']
// using implicit argument
lst.each { println it }

// using explicit argument
lst.each { val -> println val }

// both print:
// foo
// bar
// baz

Iterate with index

def lst = ['foo', 'bar', 'baz']
// explicit arguments are required
lst.eachWithIndex { val, idx -> println "$val in position $idx" }​​​​​​​​​​​​​​

// prints:
// foo in position 0
// bar in position 1
// baz in position 2

Maps

def map = [foo: 'FOO', bar: 'BAR', baz: 'BAZ']

// using implicit argument
map.each { println "key: ${it.key}, value: ${it.value}"}

// using explicit arguments
map.each { k, v -> println "key: $k, value: $v"}

// both print:
// key: foo, value: FOO
// key: bar, value: BAR
// key: baz, value: BAZ

if you want to reproduce, please indicate the source:
groovy – Collection Operators – Iterate over a collection - CodeDay