I have a HashBasedTable (com.google.common.collect.HashBasedTable
) in the following format: Table<DateTime, C, V>
. DateTime
is coming from org.joda.time.DateTime
.
I would like to group the entries based on a specific time interval. For example, if entry A and B are within 100ms of each other, I would like to group them together on the same row. I have the option to do this on insertion as well as post-insertion during processing. How should I do this in the most efficient way?
Reference links:
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
Here’s an example.
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())));
In order to use a stream I first call rowMap
to get a Map<DateTime, Map<C, V>>
, which is streamable. The stream is of map entries. I group them by a the datetime truncated to nearest 100 milliseconds. The way I truncate: If the time is 6150 ms, e.getKey().getMillisOfSecond() % 100
gives me the 50 ms, which I subtract to get 6100 ms. Thus all times from 6100 through 6199 milliseconds are grouped together. In the grouping I use a downstream collector to pick the values (the Map<C, V>
s) out of the entries for the inner lists in the result.
Disclaimer: I haven’t got Guava/Google Core Libraries installed, so I haven’t tested everything.