1

I want a Map to contain numeric mappings, I do:

Long long1 = 44l;
Long long2 = 45l;

def myMap = [long1: 26, long2: 27];

println(myMap)

But what is outputted is:

[long1:26, long2:27]

I want it to be:

[44: 26, 45: 27]

And I need to do this through variables, as I don't know what the values will be until runtime?

Thanks.

Breako Breako
  • 6,353
  • 14
  • 40
  • 59
  • Groovy does that for all variables (not only Long). If we want to use the value as key then syntax mentioned in answer has to be used. – dmahapatro Feb 11 '14 at 19:00

1 Answers1

7

Change your map declaration to the following:

def myMap = [(long1): 26, (long2): 27]

Groovy is doing some implicit variable-name-to-String conversion when you declare your Map, so instead of it evaluating long1 it is using long1 as a String key name. Also see this StackOverflow question.

Community
  • 1
  • 1
grantmcconnaughey
  • 10,130
  • 10
  • 37
  • 66