Skip to content

Instantly share code, notes, and snippets.

@tslagle13
Created March 11, 2016 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tslagle13/56004ec9461afaeacc9c to your computer and use it in GitHub Desktop.
Save tslagle13/56004ec9461afaeacc9c to your computer and use it in GitHub Desktop.
​def list1 = ["groovy","is","awesome"]
def map1 = [key1: "val1", key2: "val2"]
println "list1: $list1"
println "map1: $map1"​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
// iterate
list1.each { println "item: $it" }
​​​​​​​​map1.each { println "map item: $it" }​​​​​​​​​​​​​​​
map1.each {k,v -> println "map key: $k, val: $v"​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​}
// add
list1 << "test"
println list1​​​​​
map1 << [key3: "val3"]​​​​​​
println map1​​​
// - operator returns a new list with item removed:
def list2 = list1 - "test"
println "after remove, list1 unchanged: $list1"
println "list2: $list2"​​​​​​​​
// remove() actually removes the item:
list1.remove('test')
println "list1 after using remove() operator: $list1"
map1.remove('key3')
println "map1 remove operator removes from map: $map1​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​"
​​​​​​​
// find:
def found = list1.find {it == "groovy"}
​​​println "found in list: $found"​​​​
found = map1.find {k,v -> k == "key1"}
println "found in map: $found"​​​​​​​​
found = list1.find {it.length() > 2}
// returns first item only!
println "found in list over 2 characters: $found"​​​​​​​​​​​​​​​​​​​​​​​​​​​
​found = list1.findAll {it.length() > 2}
println "found with findAll in list over 2 chars: $found"​​​​​​​​​​​
// collect
def collectedList = list1.collect { it.toUpperCase() }
println "collectedList: $collectedList"​​​​​​​​​​​
// spread operator
println "using spread operator: ${list1*.toUpperCase()}"
// join
def sentence = list1.join(" ")
println "true fact: $sentence"​​​​​​​​​​​​​​​
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment