title: Why to synchronize on SynchronizedMap or SynchronizedCollections?
body: <p>I am referring to question asked <a href="https://stackoverflow.com/q/567068/164299">here</a> and using authors code example, now my question is </p>
  
  <ol>
  <li>Why does author uses <code>synchronized(synchronizedMap)</code>, is it really necessary because synchronizedMap will always make sure that there are no two threads trying to do <code>read/put</code> operation on <code>Map</code> so why do we need to <code>synchronize</code> on that map itself? </li>
  </ol>
  
  <p>Would really appreciate an explanation. </p>
  
  <hr>
  
  <pre><code>  public class MyClass {
    private static Map&lt;String, List&lt;String&gt;&gt; synchronizedMap =
        Collections.synchronizedMap(new HashMap&lt;String, List&lt;String&gt;&gt;());
  
    public void doWork(String key) {
      List&lt;String&gt; values = null;
      while ((values = synchronizedMap.remove(key)) != null) {
        //do something with values
      }
    }
  
    public static void addToMap(String key, String value) {
      synchronized (synchronizedMap) {
        if (synchronizedMap.containsKey(key)) {
          synchronizedMap.get(key).add(value);
        }
        else {
          List&lt;String&gt; valuesList = new ArrayList&lt;String&gt;();
          valuesList.add(value);
          synchronizedMap.put(key, valuesList);
        }
      }
    }
  }
  </code></pre>