分组 Groovy 变量 KeySet

huangapple go评论79阅读模式
英文:

Groovy GroupBy Variable KeySet

问题

List<Map> people = [
    [
        Sex: "Male",
        Age: 30,
        Job: "Programmer",
        Name: "Boris"
    ],
    [
        Sex: "Female",
        Age: 30,
        Job: "Programmer",
        Name: "Jennifer"
    ],
    [
        Sex: "Male",
        Age: 40,
        Job: "Web Designer",
        Name: "Bob"
    ],
    [
        Sex: "Female",
        Age: 20,
        Job: "Database Admin",
        Name: "Alice"
    ]
]

List results = []
if (people.groupBy({it.Sex}, {it.Age}) != groupBySpecifiedCols(people, ["Sex", "Age"])) {
  return "Failed 1"
}

if (people.groupBy({it.Sex}, {it.Age}, {it.Job}) != groupBySpecifiedCols(people, ["Sex", "Age", "Job"])) {
  return "Failed 2"
}

def groupBySpecifiedCols(List<Map> data, List<String> columns) {
  // IMPLEMENT HERE

  return null
}

return "Success!"
英文:

I am struggling with using groovy groupBy

I need to support grouping a dataset by a variable key set.

I have written a test to explain my predicament, and would love some help understanding how to implement it:

https://groovyconsole.appspot.com/script/5155615986941952

List&lt;Map&gt; people = [
    [
        Sex: &quot;Male&quot;,
        Age: 30,
        Job: &quot;Programmer&quot;,
        Name: &quot;Boris&quot;
    ],
    [
        Sex: &quot;Female&quot;,
        Age: 30,
        Job: &quot;Programmer&quot;,
        Name: &quot;Jennifer&quot;
    ],
    [
        Sex: &quot;Male&quot;,
        Age: 40,
        Job: &quot;Web Designer&quot;,
        Name: &quot;Bob&quot;
    ],
    [
        Sex: &quot;Female&quot;,
        Age: 20,
        Job: &quot;Database Admin&quot;,
        Name: &quot;Alice&quot;
    ]
]


List results = []
if(people.groupBy({it.Sex}, {it.Age}) != groupBySpecifiedCols(people, [&quot;Sex&quot;, &quot;Age&quot;])){
  return &quot;Failed 1&quot;
}

if(people.groupBy({it.Sex}, {it.Age}, {it.Job}) != groupBySpecifiedCols(people, [&quot;Sex&quot;, &quot;Age&quot;, &quot;Job&quot;])){
  return &quot;Failed 2&quot;
}

def groupBySpecifiedCols(List&lt;Map&gt; data, List&lt;String&gt; columns){
  // IMPLEMENT HERE

  return null
}


return &quot;Success!&quot;​​​​

答案1

得分: 2

实际上,我认为我刚刚解决了这个问题,在经过数小时的沮丧后,原来它很简单:

def groupBySpecifiedCols(List<Map> data, List<String> columns){
  List<Closure> groupByClosures = []
  columns.each{ column ->
    groupByClosures << { it[column] }
  }

  return data.groupBy(groupByClosures)
}

**编辑** 如评论中所指出,可能有一种更简洁的方式:

def groupBySpecifiedCols(List<Map> data, List<String> columns){
  return data.groupBy(columns.collect{ col -> ((Closure){ it[col] })})
}
英文:

Actually I think I just solved it, after hours of frustration it turned out simple:

def groupBySpecifiedCols(List&lt;Map&gt; data, List&lt;String&gt; columns){
  List&lt;Closure&gt; groupByClosures = []
  columns.each{ column -&gt;
    groupByClosures &lt;&lt; { it[column] }
  }

  return data.groupBy(groupByClosures)
}

EDIT as pointed out in comments, there is a potentially more concise way:

def groupBySpecifiedCols(List&lt;Map&gt; data, List&lt;String&gt; columns){
  return data.groupBy(columns.collect{ col -&gt; ((Closure){ it[col] })})
}

huangapple
  • 本文由 发表于 2020年4月9日 17:49:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/61118351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定