如何使用变量作为地图的关键部分 [英] How to use a variable for the key part of a map
本文介绍了如何使用变量作为地图的关键部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有
Let's say I have
def A = "abc"
def X = "xyz"
如何创建 Map
其中,而不是
def map = [A:1, X:2]
我相当于写作
I get instead the equivalent of writing
def map = [abc:1, xyz:2]
但可以使用变量 A
和
X
为键?
PS:地图值部分的同一问题。
P.S.: Same question for the value part of the map.
推荐答案
使用此:
Use this:
def map = [(A):1, (X):2]
值的一部分它更容易,因为没有automagic将文本转换为字符串发生:
For the value-part it's even easier, since there is no automagic "convert text to string" happening:
def map = [keyA:A, keyX:X]
这篇关于如何使用变量作为地图的关键部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文