Java 映射键在Groovy中是否为对象?
我是一名Java开发人员,现在正在学习groovy,但groovy正在干扰我的大脑,有些事情我需要帮助,这里列出了其中的主要部分:Java 映射键在Groovy中是否为对象?,java,groovy,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中它们是字符串值,不仅仅是带有字符串值的变量,实际上
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的资源,我确实找到了很多资源,但它们对上面提到的内容太轻率了。
简单的键在groovy中会自动转换为字符串,因为它简化了常规的映射创建 如果要从变量计算键,则需要将它们放在括号中,即:
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' ]
目前,它是,或者是Groovy In Action Book(虽然它很旧)。也是一个很好的解释来源:-)PS:Groovy wiki很旧,有些地方已经过时了。有一个可以大大改进的方法:-)