Skip to content

Instantly share code, notes, and snippets.

@hmemcpy
Forked from peysal/introduction6.groovy
Created October 3, 2018 13: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 hmemcpy/498dcc1360e859c99b21e4c9d782d397 to your computer and use it in GitHub Desktop.
Save hmemcpy/498dcc1360e859c99b21e4c9d782d397 to your computer and use it in GitHub Desktop.
introduction to groovy 1) empty map 2) adding things to map 3) concat map to map 4) iterating
def emptyMap = [:]
assert emptyMap.size() == 0
def notEmptyMap = ["person1":"john", "person2":"mus"]
assert notEmptyMap.size() == 2
notEmptyMap.put "person3","test" //adding to existing one
assert notEmptyMap.size() == 3
notEmptyMap["person4"] = "beth"
assert notEmptyMap.size() == 4
assert notEmptyMap.get("person1") == "john" //accessing the value
assert notEmptyMap.person1 == "john"
assert notEmptyMap["person1"] == "john"
notEmptyMap.each{ k,v -> println "key=${k}: value=${v}"} //iterating
notEmptyMap.each{ it -> println "key=${it.key}:value${it.value}"}
def toBeRemove = ["person3" :"oh","person4":"la"]
assert toBeRemove.each{it -> notEmptyMap.remove(it.key) }.size() == 2
notEmptyMap += toBeRemove //concat
assert notEmptyMap.size() == 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment