user1023110 user1023110 - 12 days ago 4x
Groovy Question

Groovy Grails - Problems retrieving values from Map by key

Something odd is going on in the following code. I successfully create a map, and as shown in the output I have keys and values, but I can't retrieve the values with the key.

See code and output below:

class HomeController {

def index() {
List gs=Gizmo.getAll()
def gizmoList= gs.collectEntries {
[( (]

println "item 2 is ${gizmoList['2']}"

println "item 2 is ${gizmoList[2]}"
println "keyset is " + gizmoList.keySet().toList().toString()
println "values are" + gizmoList.values().toList().toString()
[myList: gizmoList ]

the console output is:

item 2 is null
item 2 is null
keyset is [1, 2, 3]
values are[flange, nork, prule]

Somehow I think the keys maybe GStringImpl or something, but I thought putting parens around would coerce to plain strings.


In Grails, id's are Long, so your String and Integer keys are not found.

This snippet works:

assert "item 2  is ${gizmoList[2l]}" == "item 2  is nork"