我应该使用 ConcurrentHashMap 吗?

Should I use a ConcurrentHashMap?

一个关于 ConcurrentHashMap 的小问题:

public Map<String, String> getA(){
get something from db in a HashMap lets call it x
....
 do some operations in on x
....
 put the result in ConcurrentHashMap lets call it A
.....
  return A 
}

使用 ConcurrentHashMap 有意义还是我应该使用 HashMap?

1.HashMap
2.ConsurentHashMap

如果你在不同的线程上,或者数据将被同时操作(多线程委托或类似的),是的,使用 ConcurrentHashMap。否则,HashMap 应该可以(根据您提供的信息)。

基于阅读你的伪代码,我得到的印象是你在不同的线程上工作,因此 HashMap 应该 就足够了。

如果您不想担心此方法的客户端在 modifying/reading 地图时进入竞争条件,您最好将其包装在 Collections.unmodifiableMap() 中。