In Groovy I use the map literal notation quite frequently in my code, and was curious as to what concrete implementation of Map it was.
After trying a few things, this script best illustrates my confusion:
def map = ["A":"B"]
println map // I assume this avoids any lazy evaluation of the map
println map instanceof HashMap // I tried some other impls too
println map.class
and receive this output:
[A:B]
true
null
This tells me that the map is apparently a HashMap, but the getClass method doesn't want to tell me that.
So my question is: why is getClass returning null, and is there a more appropriate way to get runtime class info from Groovy?
You need to use
map.getClass()
As otherwise it's looking for a key called class
Nearly a duplicate of Why does groovy .class return a different value than .getClass()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With