Search code examples
javahashmapconcurrenthashmap

Should I use a ConcurrentHashMap?


A quick question about 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 
}

Does it make sense to have a ConcurrentHashMap or should I go with a HashMap?

1.HashMap
2.ConsurentHashMap


Solution

  • If you are on different threads or otherwise the data will be operated on at the same time (multithreaded delegate or the alike) , yes, use ConcurrentHashMap. Otherwise, HashMap should do (given the information you've provided).

    Based on reading your pseudo code, I get the impression that you are not working on different threads and therefore HashMap should suffice.