Java - 来自Guava RangeMap的get返回null,而Range存在于Range中

时间:2021-02-03 20:46:24

I tried to implemented a method to search for a string in a RangeMap. I want to check that the metricName is in the RangeMap and that it is present in a given time range.

我试图实现一个方法来搜索RangeMap中的字符串。我想检查metricName是否在RangeMap中,并且它是否存在于给定的时间范围内。

private static RangeMap<Long, String> salesRangeMap = TreeRangeMap.create();
salesRangeMap.put(
      Range.closed(1540038517L, 1540645753L), "Metric1");

public static boolean isMetricPresent(String metricName, long theKey) {
             String metricFromMap = Objects.requireNonNull(salesRangeMap.get(theKey));
    if (StringUtils.containsIgnoreCase(metricName, metricFromMap)) {
        return true;
    } 
       return false;  
}

The key in a salesRangeMap is a Range From October 20, 2018 12:28:37 PM to October 27, 2018 1:09:13 PM (in human readable format).

salesRangeMap中的关键字是2018年10月20日12:28:37 PM到2018年10月27日1:09:13 PM的范围(以人类可读的格式)。

However when I call the isMetricPresent method like this and pass it System.currentTimeMillis() as the key (which is equal to this timestamp value 1540476195830 -> October 25, 2018 2:03:15.830 PM), it returns null on this call salesRangeMap.get(theKey).

但是当我像这样调用isMetricPresent方法并将其传递给System.currentTimeMillis()作为键(它等于此时间戳值1540476195830 - > 2018年10月25日2:03:15.830 PM)时,它会在此调用salesRangeMap上返回null获得(theKey)。

long currentTime = System.currentTimeMillis(); // 1540476195830
boolean isPresent = isMetricPresent("Metric1", currentTime);

As for my current understanding, the key currentTime is present in the Range of the salesRangeMap. What is wrong in my current approach?

至于我目前的理解,关键的currentTime存在于salesRangeMap的Range中。我目前的做法有什么问题?

1 个解决方案

#1


3  

Well, it seems to me that 1540645753 < 1540476195830 - you're storing seconds in map, but query millis.

好吧,在我看来,1540645753 <1540476195830 - 您在地图中存储秒数,但查询毫秒数。

#1


3  

Well, it seems to me that 1540645753 < 1540476195830 - you're storing seconds in map, but query millis.

好吧,在我看来,1540645753 <1540476195830 - 您在地图中存储秒数,但查询毫秒数。