地图键是否是Groovy中的对象?

时间:2013-08-19 09:41:19

标签: java groovy

我是Java开发人员,现在学习groovy但是groovy正在弄乱我的大脑,我需要帮助的东西,其中的主要内容列在这里:

def map = [inm1:'hello',int2f:'world']
map.keySet().each{
println it.class.name
println "values of Key"+it.charAt(2)
}

在上面的代码中, inm1 int2f 是Java中的常规变量,但在groovy中,它们是字符串值,而不仅仅是具有字符串值的变量,它们实际上是字符串对象本身。
然而,它们是字符串然后为什么没有单个或双“或”引号。 我无法理解这个概念,我只是非常想要你的帮助 还为我提供了一些学习groovy的资源,我确实找到了很多资源,但他们过于轻率地采取上述内容。

1 个答案:

答案 0 :(得分:15)

简单的键会自动转换为Groovy中的字符串,因为它简化了常规的Map创建。

如果要评估变量中的键,则需要将它们放在括号中,即:

Integer inm1 = 10
String  int2f = 'hello'

// Regular map with string keys
assert [ inm1:'hello', int2f:'world' ] == [ 'inm1':'hello', 'int2f':'world' ]

// And evaluated keys in parentheses
assert [ (inm1):'hello', (int2f):'world' ] == [ 10:'hello', 'hello':'world' ]