title: Adding element to a HashSet inside a HashMap
body: <p>I have a HashSet which is inside a HashMap which in turn is inside another HashMap. When I try to add elements to this HashSet under one key, the elements are added to all other keys of the outer HashMap.
  Here is my code:</p>
  
  <pre><code>import java.util.HashSet;
  import java.util.HashMap;
  import java.util.Map;
  
  public class Student {
      public static void main(String args[]) {
          Map&lt;String, HashMap&lt;Integer,HashSet&lt;Integer&gt;&gt;&gt; sections
              = new HashMap&lt;String, HashMap&lt;Integer,HashSet&lt;Integer&gt;&gt;&gt;();
  
          HashMap&lt;Integer, HashSet&lt;Integer&gt;&gt; empty_map = new HashMap&lt;Integer, HashSet&lt;Integer&gt;&gt;();
          sections.put("A", empty_map);
          sections.put("B", empty_map);
          int strength = 0;
          int init = 1;
          int id1 = 1,id2 = 5;
          while (strength &lt; 3) {
  
              // Add elements only to the HashSet under the HashMap of key "A"
  
              if (! sections.get("A").containsKey(init))
                  sections.get("A").put(init, new HashSet&lt;Integer&gt;());
  
              // If the key init is already present then simply add values to it.
              sections.get("A").get(init).add(id1);
              sections.get("A").get(init).add(id2);
  
              System.out.println(sections);
  
          strength++;
          init++;
          }
      }
  
  }
  </code></pre>
  
  <p>The output of this code is:</p>
  
  <pre><code>{A={1=[1, 5]}, B={1=[1, 5]}}
  {A={1=[1, 5], 2=[1, 5]}, B={1=[1, 5], 2=[1, 5]}}
  {A={1=[1, 5], 2=[1, 5], 3=[1, 5]}, B={1=[1, 5], 2=[1, 5], 3=[1, 5]}}
  </code></pre>
  
  <p>Why are the elements getting added to the HashMap under "B" key? </p>