The code:
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;
}
The output:
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}
How is it possible?
I am quite sure that your map won't have two equal keys at the same time. What you see is an effect of modifying the map while iterating iver it (in toString()
). When the second "ALL_FX_WIRE" is written, the first one won't be present in the map any more.
You already know that HashMap
is not threadsafe. Plus eventsForThisCategory
can get modified by another thread while eventsForThisCategory.toString()
is running. So this has to be expected.
Make sure eventsForThisCategory
is not modified by multiple threads at the same time (or switch to ConcurrentHashMap
), and make sure it is not modified while toString()
is running (it is called when you create the debug output).