Groovy web console

subscribe to the feed Subscribe
to this
site

Groovy GroupBy Variable KeySet

Published 2 months ago by Sean
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
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!"