title: Assigning Hashmap to Hashmap
body: <p>I have a hashmap which I want to copy for other use. But whenever I copy it and reuse it, it also changes the original one.
  Why is that?</p>
  
  <pre><code>    do {
              Map&lt;Integer, Map&lt;String, Object&gt;&gt; map1 = originalMap; 
              //at the second iteration originalMap is the same as map1 of the last iteration, 
              //eventhough the change was nog accepted;
              //do something with map1 (change value);
              if(change is accepted) {
                 originalMap = map1;
              }
          } while(iteration &lt; 10);
  </code></pre>
  
  <p>Thanks in advance</p>
  
  <pre><code>    public static &lt;Integer,String, Schedule&gt;Map&lt;Integer, Map&lt;String, Schedule&gt;&gt; deepCopy(Map&lt;Integer, Map&lt;String, Schedule&gt;&gt; original) {
      Map&lt;Integer, Map&lt;String, Schedule&gt;&gt; copy = new HashMap&lt;Integer, Map&lt;String, Schedule&gt;&gt;();
  
      for (Map.Entry&lt;Integer, Map&lt;String, Schedule&gt;&gt; entry : original.entrySet()) {
          copy.put(entry.getKey(), deepCopy2(entry.getValue()));
      }
      return copy;
  }
  
  public static &lt;String, Schedule&gt;Map&lt;String, Schedule&gt; deepCopy2(Map&lt;String, Schedule&gt; original) {
      Map&lt;String, Schedule&gt; copy = new HashMap&lt;String, Schedule&gt;();
      for (Map.Entry&lt;String, Schedule&gt; entry : original.entrySet()) {
          copy.put(entry.getKey(), entry.getValue());
      }
  
      return copy;
  }
  </code></pre>