如何根据groovy中的条件从集合中删除节点/节点

时间:2015-03-23 15:17:00

标签: groovy

如果同一日期存在“操作值'U'和'D',我试图删除所有节点。

它应该只留下下面样本列表中前两个节点的列表,因为它没有-Date的“D”动作:2015-03-16,日期:2015-03-17,日期:2015-03- 22

尝试list.unique但是如果U& D两种行为都存在。请建议。

[
  [UserName:test, FileName:Santande.gpg, ServerPort:21, Success:true,Date:2015-03-16 06:28:46.12, Action:U],
 [UserName:test, FileName:Santande.gpg, ServerPort:21, Success:true, Date:2015-03-17 08:27:05.157, Action:U], 
 [UserName:test, FileName:Santande.gpg, ServerPort:21, Success:true, Date:2015-03-18 08:49:37.98, Action:U], 
 [UserName:test, FileName:Santande.gpg,  ServerPort:22, Success:true, Date:2015-03-18 14:00:42.23, Action:D],
 [UserName:test, FileName:Santande.gpg, ServerPort:22, Success:true, Date:2015-03-18 15:00:42.321, Action:D], 
 [UserName:test, FileName:Santande.gpg,  ServerPort:21, Success:true, Date:2015-03-19 07:12:15.616, Action:U],
 [UserName:test, FileName:Santande.gpg, ServerPort:22, Success:true, Date:2015-03-19 08:00:47.697, Action:D],
 [UserName:test, FileName:Santande.gpg, ServerPort:22, Success:true, Date:2015-03-19 09:00:48.14, Action:D],
 [UserName:test, FileName:Santande.gpg, ServerPort:22, Success:true, Date:2015-03-19 10:00:53.163, Action:D], 
 [UserName:test, FileName:Santande.gpg, ServerPort:22, Success:true, Date:2015-03-20 09:00:47.373, Action:D], 
 [UserName:test, FileName:Santande.gpg, ServerPort:21, Success:true, Date:2015-03-20 09:08:51.637, Action:U], 
 [UserName:test, FileName:Santande.gpg, ServerPort:22, Success:true, Date:2015-03-23 08:00:37.413, Action:D], 
 [UserName:test, FileName:Santande.gpg, ServerPort:22, Success:true, Date:2015-03-23 09:00:46.423, Action:D],
 [UserName:test, FileName:Santande.gpg, ServerPort:21, Success:true, Date:2015-03-23 09:06:32.997, Action:U],
 [UserName:test, FileName:Santande.gpg, ServerPort:22, Success:true, Date:2015-03-23 10:00:50.33, Action:D]
 [UserName:test, FileName:Santande.gpg, ServerPort:22, Success:true, Date:2015-03-22 10:00:50.33, Action:U]
]

1 个答案:

答案 0 :(得分:3)

按日期分组,然后选择不包含UD条目的组。 E.g:

println([
 [UserName:'test', FileName:'Santande.gpg', ServerPort:'21', Success:'true', Date:'2015-03-16 06:28:46.120', Action:'U'],
 [UserName:'test', FileName:'Santande.gpg', ServerPort:'21', Success:'true', Date:'2015-03-17 08:27:05.157', Action:'U'], 
 [UserName:'test', FileName:'Santande.gpg', ServerPort:'21', Success:'true', Date:'2015-03-18 08:49:37.980', Action:'U'], 
 [UserName:'test', FileName:'Santande.gpg', ServerPort:'22', Success:'true', Date:'2015-03-18 14:00:42.230', Action:'D'],
].groupBy{ 
    it.Date.tokenize().first() 
}.findAll{ 
    it.value*.Action.toSet()!=['U','D'].toSet() 
}*.value.flatten())