根据条件和提交日期过滤元素
Filtering elements based on condition and date filed
我正在尝试根据以下条件过滤我的列表。
检查是否有任何记录具有重复的 name
和 employee
字段。如果它有重复项,则只保留 date
字段值最新的元素并忽略其他重复值。
输入列表:
[{
name: "Bob",
employee: "yes",
date: "10/12/2020"
},
{
name: "John",
employee: "no",
date: "14/12/2020"
},
{
name: "Bob",
employee: "yes",
date: "14/12/2020"
},
{
name: "Doe",
employess: "yes",
date: "10/01/2020"
},
{
name: "Bob",
employess: "yes",
date: "09/12/2020"
},
{
name: "Doe",
employee: "yes",
date: "01/12/2019"
},
{
name: "Bob",
employee: "no",
date: "01/12/2019"
}]
理想的输出:
[{
name: "Bob",
employee: "yes",
date: "14/12/2020"
},
{
name: "John",
employee: "no",
date: "14/12/2020"
},
{
name: "Bob",
employee: "no",
date: "01/12/2019"
},
{
name: "Doe",
employess: "yes",
date: "10/01/2020"
}]
我尝试了 findAll()
方法,但无法获得所需的结果。
作为一种选择,您可以将初始列表收集为嵌套地图。
例如,首先您可以按名称对记录进行分组。然后按员工分组。最终,你会得到如下的Map。
现在您可以通过地图中的按键和列表中的最新日期来获取记录。
一个简单的 inject
会有所帮助:
def list = [[
name: "Bob",
employee: "yes",
date: "10/12/2020"
],
[
name: "John",
employee: "no",
date: "14/12/2020"
],
[
name: "Bob",
employee: "yes",
date: "14/12/2020"
],
[
name: "Doe",
employee: "yes",
date: "10/01/2020"
],
[
name: "Bob",
employee: "yes",
date: "09/12/2020"
],
[
name: "Doe",
employee: "yes",
date: "01/12/2019"
],
[
name: "Bob",
employee: "no",
date: "01/12/2019"
]]
def sdf = new java.text.SimpleDateFormat('dd/MM/yyyy')
def result = list.inject( [:] ){ res, curr ->
Date date = sdf.parse curr.date
String key = curr.name + '_' + curr.employee
if( res[ key ]?.date < date ){
res[ key ] = curr + [ date:date ]
}
res
}.values().collect{ it + [ date:sdf.format( it.date ) ] }.join('\n')
println result
打印
[name:Bob, employee:yes, date:14/12/2020]
[name:John, employee:no, date:14/12/2020]
[name:Doe, employee:yes, date:10/01/2020]
[name:Bob, employee:no, date:01/12/2019]
你可以groupBy
name
和employee
的子图,然后找到
最大 date
在分组项目的值中。例如
def data = [
[a: 1, b: 2, c: 0],
[a: 1, b: 2, c: 3],
[a: 2, b: 3, c: 0],
]
data.groupBy{
it.subMap(["a", "b"])
}.collect{ k, v ->
k + [c: v*.c.max()]
}
// -> [[a:1, b:2, c:3], [a:2, b:3, c:0]]
我正在尝试根据以下条件过滤我的列表。
检查是否有任何记录具有重复的 name
和 employee
字段。如果它有重复项,则只保留 date
字段值最新的元素并忽略其他重复值。
输入列表:
[{
name: "Bob",
employee: "yes",
date: "10/12/2020"
},
{
name: "John",
employee: "no",
date: "14/12/2020"
},
{
name: "Bob",
employee: "yes",
date: "14/12/2020"
},
{
name: "Doe",
employess: "yes",
date: "10/01/2020"
},
{
name: "Bob",
employess: "yes",
date: "09/12/2020"
},
{
name: "Doe",
employee: "yes",
date: "01/12/2019"
},
{
name: "Bob",
employee: "no",
date: "01/12/2019"
}]
理想的输出:
[{
name: "Bob",
employee: "yes",
date: "14/12/2020"
},
{
name: "John",
employee: "no",
date: "14/12/2020"
},
{
name: "Bob",
employee: "no",
date: "01/12/2019"
},
{
name: "Doe",
employess: "yes",
date: "10/01/2020"
}]
我尝试了 findAll()
方法,但无法获得所需的结果。
作为一种选择,您可以将初始列表收集为嵌套地图。
例如,首先您可以按名称对记录进行分组。然后按员工分组。最终,你会得到如下的Map
一个简单的 inject
会有所帮助:
def list = [[
name: "Bob",
employee: "yes",
date: "10/12/2020"
],
[
name: "John",
employee: "no",
date: "14/12/2020"
],
[
name: "Bob",
employee: "yes",
date: "14/12/2020"
],
[
name: "Doe",
employee: "yes",
date: "10/01/2020"
],
[
name: "Bob",
employee: "yes",
date: "09/12/2020"
],
[
name: "Doe",
employee: "yes",
date: "01/12/2019"
],
[
name: "Bob",
employee: "no",
date: "01/12/2019"
]]
def sdf = new java.text.SimpleDateFormat('dd/MM/yyyy')
def result = list.inject( [:] ){ res, curr ->
Date date = sdf.parse curr.date
String key = curr.name + '_' + curr.employee
if( res[ key ]?.date < date ){
res[ key ] = curr + [ date:date ]
}
res
}.values().collect{ it + [ date:sdf.format( it.date ) ] }.join('\n')
println result
打印
[name:Bob, employee:yes, date:14/12/2020]
[name:John, employee:no, date:14/12/2020]
[name:Doe, employee:yes, date:10/01/2020]
[name:Bob, employee:no, date:01/12/2019]
你可以groupBy
name
和employee
的子图,然后找到
最大 date
在分组项目的值中。例如
def data = [
[a: 1, b: 2, c: 0],
[a: 1, b: 2, c: 3],
[a: 2, b: 3, c: 0],
]
data.groupBy{
it.subMap(["a", "b"])
}.collect{ k, v ->
k + [c: v*.c.max()]
}
// -> [[a:1, b:2, c:3], [a:2, b:3, c:0]]