我有一个列表,我想按这些值分组。
data=[[name:'Test',job:'Dev',exp:2],
[name:'Test1',job:'Dev',exp:3]]
data.groupBy{ it.job }
结果:
[Dev:[[name:Test, job:Dev, exp:2], [name:Test1, job:Dev, exp:3]]]
预期结果:
[Dev:[exp:[2,3]]]
答案 0 :(得分:1)
仅凭您的groupBy
并不能解决问题。您需要进一步整形数据。您只对结果的所有*.exp
条目感兴趣。例如
def data=[[name:'Test',job:'Dev',exp:2],
[name:'Test1',job:'Dev',exp:3]]
println(data.groupBy{it.job}.collectEntries{[it.key, it.value*.exp]})
// => [Dev:[2, 3]]