Ivory Micky Ivory Micky - 11 months ago 74
Groovy Question

Resolving keys in groovy maps

I am wondering why the value of the key resolves to null when it is constructed from a string and a variable

def theKey="command$i"

def workingDir1 = "my/path"
def command1 = "command1"

def i=1
def theKey="command$i"

Map<String,List> map1 = new HashMap<String,String>();
map1.put("command1", workingDir1);

def value = map1.get(theKey)
println "$theKey $value"

value = map1.get(command1)
println "$command1 $value"


command1 null
command1 my/path

Is there a way to get this to work?


The problem here is different classes. def theKey="command$i" creates a GString and map1.put("command1", workingDir1); uses a String for the key.

To get your value out using theKey you have to do: