[Grails / Groovy] minimum value of map entries

Query: how can I get the minimum value from the map in Grails

So far I have found the following code to get the minimum value from the map in groovy

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

but it does not work in grails

I tried the following code snippet

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

Thanks in advance

+3
source share
2 answers

If you want to get 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

change

In addition, the closing receiving versionmap.min was in Groovy since 1.7.6, and Grails (since v1. 3.6) uses Groovy 1.7.5

+3
source

min() does not return the minimum value returned by closing the argument; it returns a collection element for which the closure returns a minimum.

map.min {it.value} - , . a MapEntry, key value . map.min{it.value}.value .

0

Source: https://habr.com/ru/post/1790927/


All Articles