2

这听起来可能很简单,但在如何使用自定义元素/键和值配对并仅打印姓氏来迭代 Map 时遇到了一些困难。任何建议都会受到欢迎!

代码

def map = ['user1':[firstname:'mark',surname:'spencer',role:"developer"],
          ['user2':[firstname:'mark',surname:'melony',role:"developer"]]

预期产出

姓氏 1:斯宾塞
姓氏 2:梅洛尼

4

1 回答 1

2

这应该这样做

def map = [ 'user1':[firstname:'mark',surname:'spencer',role:"developer"],
            'user2':[firstname:'mark',surname:'melony',role:"developer"] ]               

map.eachWithIndex { key, map2, idx ->
  println "surname ${idx + 1} - $map2.surname"
}
于 2013-01-22T08:58:20.473 回答