groovy transform list into map using a groupBy max -
ok, rather simple thing not able figure out. have groovy list objects in like:
def listoftings = [ ["type": "a", "value": 1], ["type": "b", "value": 3], ["type": "a", "value": 2], ["type": "b", "value": 2] ] and want map out of groupby(type) max(value):
["a": 2, "b": 3] but can not find groupby method , have not figured out using collectentries. there standard way achieve such result?
try following:
listoftings.groupby { it.type }.collectentries { [(it.key): it.value.max { it.value }.value] } first groupby create map of lists grouped type field:
[a:[[type:a, value:1], [type:a, value:2]], b:[[type:b, value:3], [type:b, value:2]]]
next, collectentries it.value.max { it.value } pick single entry maximum value field:
[a:[type:a, value:2], b:[type:b, value:3]]
picking value it.value.max { it.value }.value extract value field instead of whole entry:
[a:2, b:3]
hope helps :)
Comments
Post a Comment