Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added map questions to FAQ


Code Block
def x = [ "a", "qrs" ] as String[]

Why does myMap.size or myMap.class return null?

In Groovy, maps override the dot operator to behave the same as the index[ ] operator. So myMap.size is the same as calling myMap["size"].
Use this instead: myMap.@size or myMap.size(). See the User Guide.

Why is my map returning null values?

Chances are, you tried to use a variable as a key in a map literal definition.
Remember, keys are interpreted as literal strings:

Code Block
myMap = [myVar:"one"];
assert myMap["myVar"] == "one"

Try this (note the parentheses around the key):

Code Block
myMap = [(myVar):"one"]