In a multi-threaded application, is there any scenario in which it is better to use a HashMap with synchronization when needed, than to use a ConcurrentHashMap?
Specifically, I am thinking of an application where the map is initialized like this:
Map<String,String> map = new HashMap<>();
Each thread accesses the map only to update it and immediately convert it to string, so the only synchronized block is:
synchronized(map) {
map.put(key,value);
StringBuilder sb = new StringBuilder();
for (String k: map.keySet())
sb.append("("+k+","+map.get(k)+")");
}
In this case, will it be safer to change the initialization to:
Map<String,String> map = new ConcurrentHashMap<>();
and drop the "synchronized"?