-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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