我对Java略知一二,但对groovy一无所知。我使用我的一些常规知识并使用groupBy集合操作results.groupBy({ plan -> plan.planItem.self })来达到以下结果。
我想比较并获得以startDate结尾的planItem.self URL的最早的planItem.self和最新的TEST-2。您可以看到,TEST-2有两个具有不同startDate和endDate的条目。我想要的最后一个对象只有一个TEST-2条目,但如上面提到的日期,并删除除一个以外的所有对象。类似地,对于所有的页面,比如AP-3、AP-1。我有一个集合对象,如下所示(您可能会注意到,TEST-2和AP-3有多个对象)。
{
"https://dev.example.com/pages/TEST-2": [
{
"startDate": "2020-05-16",
"endDate": "2020-05-17",
"planItem": {
"self": "https://dev.example.com/pages/TEST-2",
"type": "ISSUE"
},
},
{
"startDate": "2020-05-17",
"endDate": "2020-05-20",
"planItem": {
"self": "https://dev.example.com/pages/TEST-2",
"type": "ISSUE"
}
}
],
"https://dev.example.com/pages/AP-3": [
{
"startDate": "2020-05-16",
"endDate": "2020-05-17",
"planItem": {
"self": "https://dev.example.com/pages/AP-3",
"type": "ISSUE"
}
},
{
"startDate": "2020-05-27",
"endDate": "2020-05-27",
"planItem": {
"self": "https://dev.example.com/pages/AP-3",
"type": "ISSUE"
}
}
],
"https://dev.example.com/pages/AP-1": [
{
"startDate": "2020-05-16",
"endDate": "2020-05-17",
"planItem": {
"self": "https://dev.example.com/pages/AP-1",
"type": "ISSUE"
}
}
],
"https://dev.example.com/pages/AP-4": [
{
"startDate": "2020-05-16",
"endDate": "2020-05-18",
"planItem": {
"self": "https://dev.example.com/pages/AP-4",
"type": "ISSUE"
}
}
]
}我想要创建的输出对象如下所示(您可能会注意到,在输出集合中,只有一个对象用于TEST-2和AP-3),并且删除了不必要的属性。请指导我使用最好的Groovy操作,使整个过程更快,因为我只有60秒的时间来运行这些操作以及REST调用,一旦我以我想要的格式准备好这些对象,那么处理时间就非常重要。
{
"https://dev.example.com/pages/TEST-2": [
{
"startDate": "2020-05-16",
"endDate": "2020-05-20"
}
],
"https://dev.example.com/pages/AP-3": [
{
"startDate": "2020-05-16",
"endDate": "2020-05-27"
}
],
"https://dev.example.com/pages/AP-1": [
{
"startDate": "2020-05-16",
"endDate": "2020-05-17"
}
],
"https://dev.example.com/pages/AP-4": [
{
"startDate": "2020-05-16",
"endDate": "2020-05-18"
}
]
}日期应从字符串转换为日期。
发布于 2020-05-26 19:37:12
你已经有了你的小组的基本结构了。接下来,要为每个映射值提供所有startDate的最小值和所有endDate的最大值。例如,这项工作:
def data = new groovy.json.JsonSlurper().parse("data.json" as File)
println(data.collectEntries{ url, plans ->
[url, [startDate: plans*.startDate.min(), endDate: plans*.endDate.max()]]
}.inspect())
// → ['https://dev.example.com/pages/TEST-2':['startDate':'2020-05-16', 'endDate':'2020-05-20'], 'https://dev.example.com/pages/AP-3':['startDate':'2020-05-16', 'endDate':'2020-05-27'], 'https://dev.example.com/pages/AP-1':['startDate':'2020-05-16', 'endDate':'2020-05-17'], 'https://dev.example.com/pages/AP-4':['startDate':'2020-05-16', 'endDate':'2020-05-18']]这将创建一个新的映射,保持原来的键,并从列表中创建一个新的映射作为值,其中所有startDate:s都会被收集(通过spread操作符),然后得到最小值(字符串作为日期已经可以很好地排序)。对于最大的endDate也是如此。
https://stackoverflow.com/questions/62029855
复制相似问题