Map
is an interface, so the answer depends on the implementation you're using.
Generally speaking, the simpler implementations of this interface, such as HashMap
and TreeMap
are not thread safe. If you don't have some synchronization built around them, concurrently put
ing and get
ing will result in an undefined behavior - you may get the new value, you may get the old one, bust most probably you'd just get a ConcurrentModificationException
, or something worse.
If you want to handle the same Map
from different threads, either use one of the implementations of a ConcurrentMap
(e.g., a ConcurrentHashMap
), which guarantees a happens-before-sequence (i.e., if the get
was fired before the put
, you'd get the old value, even if the put
is ongoing, and vise-versa), or synchronize the Map
's access (e.g., by calling Collections#synchronizedMap(Map)
.