title: Overridden equals not mandatory for efficient hashing keys
body: <p>In all questions i saw about overridden equals and hashcode methods people always say if you override equals method you should override also hashcode method and vice versa in order to avoid problems while getting objects from Hash collections.</p>
  
  <p>Personally i don't see that the vice versa is right for that purpose, Let's consider that we use an object(with attributes) as keys in a HashMap and we don't need to test the equality between two instances of that object.</p>
  
  <p>If we override the hashcode method in an efficient manner(based on attributes &amp; other rules), so we can have unique keys, in this case for the HashMap we gonna have a unique value in each bucket, and the HashMap will not use the equals method to compare values in a bucket since we have one value in each bucket.</p>
  
  <p>the contract side:</p>
  
  <ul>
  <li>two instances of our object are equals(the Object.equals one) means they have the same reference, and based on our hashcode method the hashcode will be the same ==> OK</li>
  <li>different hashcodes leads to not equal objects ==> NOK (in our case it is possible to break this rule) but since we have good hashing keys(our scenario purpose) the contract doesn't really matter</li>
  </ul>
  
  <p>Am I wrong?</p>
  
  <p><strong>Edit :</strong> (after search, below answers, and looking in the source code of HashMap)</p>
  
  <p>Why should we override equals method also?</p>
  
  <p>Initial conditions, unique hashcodes and no equals() overridden</p>
  
  <ul>
  <li>Retrieving values using get() method problem:</li>
  </ul>
  
  <p>Object.equals() perform a reference comparison, and this method is used in the methods put and get</p>
  
  <pre><code>public V get(Object key) {
      if (key == null)
          return getForNullKey();
      int hash = hash(key.hashCode());
      for (Entry&lt;K,V&gt; e = table[indexFor(hash, table.length)];
              e != null;
              e = e.next) {
          Object k;
          if (e.hash == hash &amp;&amp; ((k = e.key) == key || key.equals(k)))
              return e.value;
      }
      return null;
  }
  </code></pre>
  
  <p>If we don’t override equals we will not be able to retrieve values from the HashMap if we lose the reference of the keys, e.g. of keys references which are not visible to another code. 
  The only way to read the HashMap is to use an Iterator, plus comparing the values to retrieve the exact pair:</p>
  
  <pre><code>First put: [key:Instance1Obj(144756696),  value:Person1Obj(“Baron”, “Steven”)]
  Second put: [key:Instance2Obj(17488696),  value:Person2Obj(“Hewlett”, “Emily”)]
  </code></pre>
  
  <p>If we want to retrieve the account of Baron Steven based on his account number, we can’t only just create a new object having the same account number say Instance88Obj(144756696) and invoke the get method to retrieve the value. In this case we will have a null as a result because the get method uses a comparison based on the reference of the key, even if the hashcode will be the same.</p>
  
  <ul>
  <li>Updating  values using put() method problem:</li>
  </ul>
  
  <p>if equals is not overridden, putting a new value through a new instance key(but having same hashcode) in order to update it, will not work, just another pair key/value will be added:</p>
  
  <pre><code>First put: [key:Instance1Obj(144756696),  value:Person1Obj(“Baron”, “Seven”)]
  Second put: [key:Instance2Obj(144756696),  value:Person2Obj(“Baron”, “Steven”)]
  </code></pre>
  
  <p>Normally we are expecting to substitute the value “Seven” by “Steven” but the substitution will not happen </p>
  
  <pre><code>public V put(K key, V value) {
      if (key == null)
          return putForNullKey(value);
      int hash = hash(key.hashCode());
      int i = indexFor(hash, table.length);
      for (Entry&lt;K,V&gt; e = table[i]; e != null; e = e.next) {
          Object k;
          if (e.hash == hash &amp;&amp; ((k = e.key) == key || key.equals(k))) {
              V oldValue = e.value;
              e.value = value;
              e.recordAccess(this);
              return oldValue;
          }
      }
  
      modCount++;
      addEntry(hash, key, value, i);
      return null;
  }
  </code></pre>
  
  <blockquote>
    <p>Conclusion :</p>
    
    <p>==> If you override hashcode, override equals also.</p>
    
    <p>==> If you override equals, override hashcode also.(out of this topic :))</p>
  </blockquote>