-
StackOverflow 文档
-
groovy 教程
-
收集运算符
-
迭代一个集合
清单
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
用索引迭代
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
地图
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