开发者

[Grails/Groovy]minimum value of Map entries

Query: how can i pul开发者_如何转开发l minimum value from a map in Grails

So far i have found following code to get minimum value from a map in groovy

["Java":1, "Groovy":4, "JavaScript":2].min{it.value}

but it donot work in Grails

i have tried following piece of code

def map = ["Java":1, "Groovy":4, "JavaScript":2]
println map.min{it.value}
assert map.min{it.value}==1

Thanks in advance


If you want the minimum value from the map, you can do:

def map = ["Java":1, "Groovy":4, "JavaScript":2]
println map.values().min()
assert map.values().min() == 1

edit

Also, the closure accepting version of map.min has been in Groovy since 1.7.6, and Grails (as of v1.3.6) uses Groovy 1.7.5


min() doesn't return a minimum value returned by argument closure, it returns the element of a collection for which the closure returns minimum.

map.min {it.value} is valid call, but it's not a value. It's a MapEntry, with key and value properties. So map.min{it.value}.value would do.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜