Groovy/Grails : 如何将多个key组合成groupBy?
Groovy/Grails : How to groupBy multiple keys combined?
我有一个列表,我想按所有三个键进行分组,我指的是 How to group a list of list。
def given = [
[Country:'Japan',Flag:'Yes',Event:'New Year'],
[Country:'china',Flag:'No',Event:'Spring Festival'],
[Country:'uk',Flag:'No',Event:'National Holiday'],
[Country:'us',Flag:'Yes',Event:'Labour Day'],
[Country:'us',Flag:'Yes',Event:'New Year'],
[Country:'uk',Flag:'Yes',Event:'Memorial Day']
]
我们可以分组:
def mapped = given.groupBy {
[(it["Country"]) : it["Flag"] ] }
如何按 [(it["Country"]) : it["Flag"] : it["Event"] ]
分组?
预期结果:[['Japan':['Yes':[NewYear]]]:[['Country':'Japan', 'Flag':'Yes', 'Event':'New Year']]
,..
given.groupBy( { it.Country }, { it.Flag }, { it.Event } )
一个以 3 个闭包作为参数的方法。
这个有什么用,不懂。 @dmahapatro 的解决方案提供了更易于处理的结果。在您的示例中,您只想将递归映射作为分组依据的键。我最怀疑这会很好地处理实际的分组情况。
def given = [
[Country:'Japan',Flag:'Yes',Event:'New Year'],
[Country:'china',Flag:'No',Event:'Spring Festival'],
[Country:'uk',Flag:'No',Event:'National Holiday'],
[Country:'us',Flag:'Yes',Event:'Labour Day'],
[Country:'us',Flag:'Yes',Event:'New Year'],
[Country:'uk',Flag:'Yes',Event:'Memorial Day']
]
println given.groupBy{ [(it.Country): [(it.Flag): [it.Event]]] }.inspect()
//=> [['Japan':['Yes':['New Year']]]:[['Country':'Japan', 'Flag':'Yes', 'Event':'New Year']], ...
我有一个列表,我想按所有三个键进行分组,我指的是 How to group a list of list。
def given = [
[Country:'Japan',Flag:'Yes',Event:'New Year'],
[Country:'china',Flag:'No',Event:'Spring Festival'],
[Country:'uk',Flag:'No',Event:'National Holiday'],
[Country:'us',Flag:'Yes',Event:'Labour Day'],
[Country:'us',Flag:'Yes',Event:'New Year'],
[Country:'uk',Flag:'Yes',Event:'Memorial Day']
]
我们可以分组:
def mapped = given.groupBy {
[(it["Country"]) : it["Flag"] ] }
如何按 [(it["Country"]) : it["Flag"] : it["Event"] ]
分组?
预期结果:[['Japan':['Yes':[NewYear]]]:[['Country':'Japan', 'Flag':'Yes', 'Event':'New Year']]
,..
given.groupBy( { it.Country }, { it.Flag }, { it.Event } )
一个以 3 个闭包作为参数的方法。
这个有什么用,不懂。 @dmahapatro 的解决方案提供了更易于处理的结果。在您的示例中,您只想将递归映射作为分组依据的键。我最怀疑这会很好地处理实际的分组情况。
def given = [
[Country:'Japan',Flag:'Yes',Event:'New Year'],
[Country:'china',Flag:'No',Event:'Spring Festival'],
[Country:'uk',Flag:'No',Event:'National Holiday'],
[Country:'us',Flag:'Yes',Event:'Labour Day'],
[Country:'us',Flag:'Yes',Event:'New Year'],
[Country:'uk',Flag:'Yes',Event:'Memorial Day']
]
println given.groupBy{ [(it.Country): [(it.Flag): [it.Event]]] }.inspect()
//=> [['Japan':['Yes':['New Year']]]:[['Country':'Japan', 'Flag':'Yes', 'Event':'New Year']], ...