Ivory Micky
Ivory Micky

Reputation: 581

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"

Output:

command1 null
command1 my/path   

Is there a way to get this to work?

Upvotes: 0

Views: 57

Answers (1)

doelleri
doelleri

Reputation: 19702

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:

map1.get(theKey.toString())

Upvotes: 1

Related Questions