我有一个列表,如:
def given = [
[Country:'Japan',Flag:'Yes',Event:'New Year', Amount:70],
[Country:'china',Flag:'No',Event:'Spring Festival', Amount:60],
[Country:'us',Flag:'Yes',Event:'Labour Day', Amount:10],
[Country:'us',Flag:'Yes',Event:'Labour Day', Amount:20]
]
我期望的是:
[
[Country:'Japan',Flag:'Yes',Event:'New Year', Amount:70],
[Country:'china',Flag:'No',Event:'Spring Festival', Amount:60],
[Country:'us',Flag:'Yes',Event:'Labour Day', Amount:30]
]
最佳答案
另一种可能性(但结果完全相同)
given.groupBy { it.subMap('Country', 'Flag', 'Event') }
.collect { k, v -> k + [Amount: v.Amount.sum()] }
关于groovy groupby 多个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40100943/