Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.


Code Block
def map4= [:]
map4[ 1 ]= 'a'
map4[ 2 ]= 'b'
map4[ true ]= 'p' //we can use boolean values as a key
map4[ false ]= 'q'
map4[ null ]= 'x' //we can also use null as a key
map4[ 'null' ]= 'z'
assert map4 == [1:'a', 2:'b', (true):'p', (false):'q', (null):'x', 'null':'z' ]

To use the value of a String as the key value of a map, simply surround the variable with parenthesis. 

Code Block

def foo = "test"
def map =  [(foo):"bar"]

println map // will output ["test":"bar"]
map = [foo:"bar"]
println map // will output ["foo":"bar"] 

We can use each() and eachWithIndex() to access keys and values: