Loop through Map in Groovy?

后端 未结 4 418
余生分开走
余生分开走 2021-01-30 02:11

I have a very simple task I am trying to do in Groovy but cannot seem to get it to work. I am just trying to loop through a map object in groovy and print out the key and value

4条回答
  •  故里飘歌
    2021-01-30 02:30

    Alternatively you could use a for loop as shown in the Groovy Docs:

    def map = ['a':1, 'b':2, 'c':3]
    for ( e in map ) {
        print "key = ${e.key}, value = ${e.value}"
    }
    
    /*
    Result:
    key = a, value = 1
    key = b, value = 2
    key = c, value = 3
    */
    

    One benefit of using a for loop as opposed to an each closure is easier debugging, as you cannot hit a break point inside an each closure (when using Netbeans).

提交回复
热议问题