Skip to content

Instantly share code, notes, and snippets.

@peysal
Created August 25, 2013 10:28
Show Gist options
  • Select an option

  • Save peysal/6333149 to your computer and use it in GitHub Desktop.

Select an option

Save peysal/6333149 to your computer and use it in GitHub Desktop.

Revisions

  1. peysal created this gist Aug 25, 2013.
    21 changes: 21 additions & 0 deletions introduction6.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    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