Use this:
def map = [(A):1, (X):2]
For the value-part it's even easier, since there is no automagic "convert text to string" happening:
def map = [keyA:A, keyX:X]
Further to Joachim's answer, if you want to add entries to an existing map and the keys are variables, use:
def map = [:]
def A = 'abc'
map[A] = 2
If you use:
map.A = 2
It is assumed that you want to use the literal string 'A' as the key (even though there is a variable named A in scope.
As @tim_yates pointed out in a comment, a key variable will also be resolved if you use:
map."$A" = 2
though personally I prefer to use the [A]
syntax because refactoring tools might miss the "$A"
reference if the variable is renamed
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With