gpt4 book ai didi

groovy groupby 多个字段

转载 作者:行者123 更新时间:2023-12-03 22:58:06 26 4
gpt4 key购买 nike

我有一个列表,如:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com