使用Groovy中的自定义元素迭代Map?

时间:2013-01-22 07:43:43

标签: groovy

这可能听起来很简单,但是我很难解决如何使用自定义元素/键和值配对迭代地图并仅打印姓氏。任何建议都会受到欢迎!

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

预期输出

姓氏1:斯宾塞
姓氏2:melony

1 个答案:

答案 0 :(得分: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"
}