根据 DateTime 行对 HashBasedTable 条目进行分组

Grouping HashBasedTable entries based on DateTime row

我有一个 HashBasedTable (com.google.common.collect.HashBasedTable),格式如下:Table<DateTime, C, V>DateTime 来自 org.joda.time.DateTime

我想根据特定时间间隔对条目进行分组。例如,如果条目 A 和 B 彼此相差在 100 毫秒以内,我想将它们组合在同一行中。我可以选择在插入时执行此操作,也可以在处理期间执行 post-insertion。我应该如何以最有效的方式做到这一点?

参考链接:

https://google.github.io/guava/releases/19.0/api/docs/com/google/common/collect/HashBasedTable.html

https://www.joda.org/joda-time/apidocs/org/joda/time/DateTime.html

举个例子。

    Table<DateTime, String, String> yourTable = // your HashBasedTable;
    Map<DateTime, List<Map<String, String>>> groupedRows = yourTable.rowMap()
            .entrySet()
            .stream()
            .collect(Collectors.groupingBy(e -> e.getKey().minusMillis(e.getKey().getMillisOfSecond() % 100),
                    Collectors.mapping(Map.Entry::getValue, Collectors.toList())));

为了使用流,我首先调用 rowMap 以获得 Map<DateTime, Map<C, V>>,它是可流式传输的。流是映射条目。我按截断到最接近 100 毫秒的日期时间对它们进行分组。我截断的方式:如果时间是 6150 毫秒,e.getKey().getMillisOfSecond() % 100 给我 50 毫秒,我减去它得到 6100 毫秒。因此,从 6100 到 6199 毫秒的所有时间都组合在一起。在分组中,我使用 下游收集器 从结果中的内部列表的条目中挑选值(Map<C, V>s)。

免责声明:我还没有安装 Guava/Google 核心库,所以我没有测试所有内容。