Groovy web console

subscribe to the feed Subscribe
to this
site
Collect vs each (via #groovywebconsole)
tweet this snippet Tweet
this
script

Collect vs each

Published 8 months ago by Jackie
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
class MyClass {
    Boolean flag
}

configs = [[foo: 'bar'], [foo: 'baz', flag: true], [foo: 'qux', flag: false]]

eachItems = configs.each {
    new MyClass(flag: it.containsKey('flag') ? it.flag : true)
}

collectItems = configs.collect {
    new MyClass(flag: it.containsKey('flag') ? it.flag : true)
}

println(eachItems)
eachItems.each { println(it.flag) }

println(collectItems)
collectItems.each { println(it.flag) }