title: Why is equals method not called in the Hashmap?
body: <pre><code>import java.util.*;
  class Dog {
      public Dog(String n) { name = n; }
      public String name;
      public boolean equals(Object o) {
          System.out.println("equals called..");
          if((o instanceof Dog) &amp;&amp;
                  (((Dog)o).name == name)) {
              return true;
          } else {
              return false;
          }
      }
      public int hashCode() {
          System.out.println("hashCodecalled..");
          return name.length(); 
      }
  }
  class SampleClass {
      public static void main(String[] args) {
          Map&lt;Object, String&gt; m = new HashMap&lt;Object, String&gt;();
          Dog d1 = new Dog("clover");
          m.put(d1, "Dog key");
          d1.name ="arthur";
          System.out.println(m.get(d1));      
      }
  }
  </code></pre>
  
  <p>In the above code, the output i get is:</p>
  
  <pre><code>hashCodecalled..
  hashCodecalled..
  Dog key
  </code></pre>
  
  <p>after i do d1.name ="arthur"</p>
  
  <p>i was expecting output to be</p>
  
  <pre><code>hashCodecalled..``
  hashCodecalled..
  equals called..
  null
  </code></pre>