使用闭包 groovy 在地图内编写 DSL 地图,

发布于 2024-12-05 07:47:38 字数 486 浏览 1 评论 0原文

使用闭包访问地图内的地图, 我有一个地图对象,其值是另一个地图对象 例如:- `

 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 ->
        println entry.key
        println entry.value
    }

'

如何以简单的任何提示为上面的地图示例编写 DSL?

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 ->
        println entry.key
        println entry.value
    }

'

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖公主 2024-12-12 07:47:38

这是打印内部映射的键和值的示例。试试这个:

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}"
    }
}

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}"
    }
}
分分钟 2024-12-12 07:47:38

anish,我假设您正在寻找一种更短的方式来访问地图,这将是 map2.map1。然后您可以使用map2.map1.name来获取“Gromit”。如果您不希望以更短的方式获取地图,请指定更多信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文