Gotcha: map keys in Groovy

Key equality is tricky, let's go shopping.

Maps are great quick-and-dirty data structures or caches in many programming languages. But there's a least one way that Groovy maps aren't so friendly. Observe:

groovysh> m1 = [1:1]
===> {1=1}
groovysh> m1[1]
===> 1
groovysh> m1[(int) 1]
===> 1
groovysh> m1[(long) 1]
===> null

So, map keys are using a strict form of equality where type matters, so that an integer doesn't equal a long. To demonstrate further (note the wacky notation to force types when initialising the map):

groovysh> m2 = [((long) 1):1]
===> {1=1}
groovysh> m2[1]
===> null
groovysh> m2[(int) 1]
===> null
groovysh> m2[(long) 1]
===> 1