 |
Subscribe to this site |
|
05 Maps

Published 6 months ago
by
ST
with tags
groovy
// Maps are keys associated with values
// empty map
def emptyMap = [:]
// maps with string keys
def strMap = [Sweden: 'Stockholm', Denmark: 'Kopenhagen', Belgium: 'Brussels']
// retrieve items via key
def cityDenmark = strMap['Denmark']
// string keys support notation w/dot
def citySweden = strMap.Sweden
// add an item
strMap['Germany'] = 'Berlin'
// string keys support notation w/dot
strMap.France = 'Paris'
// Groovy adds a lot of useful methods
// see http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Map.html
// e.g. looping with each
strMap.each { country, city ->
println "The capital of $country is $city"
}