我有一个类型为ConcurrentSkipListMap
的全局变量,定义如下:
static ConcurrentNavigableMap<ZonedDateTime, String> notificationMap = new ConcurrentSkipListMap<ZonedDateTime, String>(Comparator.comparingLong(v -> v.toInstant().toEpochMilli()));
我要对这张地图进行以下操作
我还想删除3分钟以上的项目。所以我的实现是这样的
private Boolean isDuplicateNotification(String hMac, String jsonString) {
try (JsonReader jsonReader = Json.createReader(new StringReader(jsonString))) {
notificationMap = getEventsFromLastthreeMinutes();
if (notificationMap.containsValue(hMac))
return true;
return false;
}
}
private void storeNotification(String hMac) {
WebHookEvent event = new WebHookEvent(eventTime, hMac);
notificationMap.put(event.eventTime, event.hmac);
}
public static ConcurrentNavigableMap<ZonedDateTime, String> getEventsFromLastthreeMinutes() {
return notificationMap.tailMap(ZonedDateTime.now().minusMinutes(3));
}
如果我在3分钟后收到新商品,我的地图就会变空(如果在3分钟内仅收到1件商品),并且在storeNotification事件中,它会返回以下异常
SRVE0777E:引发异常
应用程序类 'java.util.concurrent.ConcurrentSkipListMap $ SubMap.checkKeyBounds:2,435' [INFO] java.lang.IllegalArgumentException:键超出 rangejava.base / java.util.concurrent.ConcurrentSkipListMap $ SubMap.checkKeyBounds(ConcurrentSkipListMap.java:2435)
但是如果我再发送一次。.
它将毫无问题地添加到地图中。我该如何实现?我们可以在不重新分配地图的情况下从地图中删除多个项目吗? 请帮我。 谢谢!