简体   繁体   English

根据 Java groovy 中 map 内的值从地图列表中筛选出地图

[英]Filter out maps from a list of maps based on values inside the map in Java groovy

I have a list of maps, with 3 key/values like this:我有一个地图列表,其中包含 3 个键/值,如下所示:

[
  [name: name1, date: Sat Oct 01 00: 00: 00 CEST 2021, doc: doc1],
  [name: name2, date: Sun Oct 14 00: 00: 00 CEST 2021, doc: doc1],
  [name: name2, date: Sat Oct 23 00: 00: 00 CEST 2021, doc: doc2],
//[name: name2, date: Sat Oct 15 00: 00: 00 CEST 2021, doc: doc2],
  [name: name2, date: Sat Oct 04 00: 00: 00 CEST 2021, doc: doc3],
  [name: name2, date: Sat Oct 11 00: 00: 00 CEST 2021, doc: doc4],
  [name: name3, date: Sat Oct 20 00: 00: 00 CEST 2021, doc: doc1],
  [name: name3, date: Sat Oct 03 00: 00: 00 CEST 2021, doc: doc2],
]

I need to find the most recent date for any given combination of name and doc, so for example if i have two docs of the same type for any given name i need to filter out all but the one with the most recent date.我需要为任何给定的名称和文档组合找到最近的日期,因此例如,如果我有两个相同类型的任何给定名称的文档,我需要过滤掉除具有最近日期的文档之外的所有文档。

In this case, I need to filter out the commented out line as another map with the same kind of name and doc exist with more recent date.在这种情况下,我需要过滤掉注释掉的行,因为另一个 map 具有相同的名称和文档,但日期更近。

How can I accomplish this using a list of maps in Java Groovy?我如何使用 Java Groovy 中的地图列表来完成此操作?

You can groupBy your list over the values of the keys you want to group (eg name and doc ).您可以对要分组的键的值(例如namedoc )进行groupBy列表。 Then collectMany over the resulting map: if there is only one item in the value , return it;然后对结果collectMany进行 collectMany:如果value中只有一项,则返回; otherwise reverse sort the items by the values of your sort-key (eg date ) and return the tail .否则按排序键的值(例如date )对项目进行反向sort并返回tail

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM