开发者

writing DSL map inside map with closure groovy,

Accessing map inside map with closure, I have a map object the values is another map object e.g:- `

 to access the data like this I can issue
def map = [name:"Gromit", likes:"cheese", id:1234]
def map2 =[map1:map]
map2.each{entry ->
    println entry.key
    entry.value.each {entry1 ->  println  entry1.key
        println entry1.value
    }
}


to access a single map i can issue 
    map.each{entry开发者_Python百科 ->
        println entry.key
        println entry.value
    }

'

How can I write a DSL for the above map example in simple any hint?


Here is an illustration of printing the keys and values of the inner map. Try this:

map1=new HashMap()
map2=new HashMap()
map2.put("1","one")
map1.put("map2",map2) 

map1.each{ entry1 ->
    def innerMap = entry1.value
    innerMap.each { entry2 ->
        println "key is ${entry2.key}"
        println "value is ${entry2.value}"
    }
}


anish, I assume you look for a shorter way to access the map, this would be map2.map1. You can then use map2.map1.name to get "Gromit". If a shorter way to get the map was not your question, then please specify more.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜