如何解释 Java Hashmap 中的重复条目?

How to explain duplicate entries in Java Hashmap?

  1. 这是在多线程下
  2. 我已阅读有关 HashMap 的主题,但找不到相关内容 questions/answers

代码:

 private Map<String, String> eventsForThisCategory;
    ...
    Map<String, String> getEventsForThisCategory() {
        if (eventsForThisCategory == null) {
            Collection<INotificationEventsObj> notificationEvents = notificationEventsIndex.getCategoryNotificationEvents(getCategoryId());
            eventsForThisCategory = new HashMap<String, String>();
            for(INotificationEventsObj notificationEvent : notificationEvents) {
                eventsForThisCategory.put(notificationEvent.getNotificationEventID(), notificationEvent.getNotificationEventName());
            }
            logger.debug("eventsForThisCategory is {}", eventsForThisCategory);
        }
        return eventsForThisCategory;
    }

输出:

app_debug.9.log.gz:07 Apr 2016 13:47:06,661 DEBUG [WirePushNotificationSyncHandler::WirePushNotification.Worker-1] - eventsForThisCategory is {FX_WIRE_DATA=Key Economic Data, ALL_FX_WIRE=All, ALL_FX_WIRE=All, FX_WIRE_HEADLINES=Critical Headlines}

怎么可能?

HashMap 在多线程中不是线程安全的。 你可以尝试使用 Collections.synchronizedMap() 来包装你的 hashMap 实例 或者使用 ConcurrentHashMap 可能更好

我很确定您的地图不会同时有两个相同的键。您看到的是在迭代地图时修改地图的效果(在 toString() 中)。当第二个 "ALL_FX_WIRE" 写入时,第一个将不再出现在地图中。

您已经知道 HashMap 不是线程安全的。另外 eventsForThisCategory 可以被另一个线程修改,而 eventsForThisCategory.toString() 是 运行。所以这是意料之中的事情。

确保eventsForThisCategory不被多个线程同时修改(或切换到ConcurrentHashMap),并确保在toString()为[=25]时不被修改=](创建调试输出时调用)。