title: Updating Java HashMap key
body: <p>I was just wondering, what would happen if key of a <code>HashMap</code> is mutable, test program below demonstrate that and I am unable to understand when both equals and <code>hashCode</code> methods returns 
  true and same value, why does <code>hashmap.containsKey</code> return <code>false</code>.</p>
  
  <pre><code>public class MutableKeyHashMap {
  
      public static void main(String []a){
  
              HashMap&lt;Mutable, String&gt; map = new HashMap&lt;Mutable, String&gt;();
              Mutable m1 = new Mutable(5);
              map.put(m1, "m1");
              Mutable m2 = new Mutable(5);
              System.out.println(map.containsKey(m2));    
  
              m2.setA(6);
              m1.setA(6);
              Mutable m3 = map.keySet().iterator().next();
  
              System.out.println(map.containsKey(m2)+"    "+m3.hashCode()+"       "+m2.hashCode()+"       "+m3.equals(m2));   
  
      }
  }
  class Mutable {
  
      int a;
  
      public Mutable(int a) {
  
          this.a = a;
      }
  
      @Override
      public boolean equals(Object obj) {
  
          Mutable m = (Mutable) obj;
          return m.a == this.a ? true : false; 
      }
  
      @Override
      public int hashCode(){
          return a;
      }
  
      public void setA(int a) {
  
          this.a = a;
      }
  
      public int getA() {
          return a;
      }
  } 
  </code></pre>
  
  <p>This the output :</p>
  
  <blockquote>
    <p>true
    false 6       6       true</p>
  </blockquote>