title: Why keys are compared in a strange way in HashMap.put()?
body: <p>While skimming through the code of put() of HashMap, I came across a strange piece of code. Consider the below excerpt of code:</p>
  
  <pre><code>490  public V put(K key, V value) {
  491        if (table == EMPTY_TABLE) {
  492            inflateTable(threshold);
  493     }
  494  if (key == null)
  495   return putForNullKey(value);
  496     int hash = hash(key);
  497     int i = indexFor(hash, table.length);
  498     for (Entry&lt;K,V&gt; e = table[i]; e != null; e = e.next) {
  499         Object k;
  500         if (e.hash == hash &amp;&amp; ((k = e.key) == key || key.equals(k))) {
  501             V oldValue = e.value;
  502             e.value = value;
  503             e.recordAccess(this);
  504             return oldValue;
  505         }
  506     }
  </code></pre>
  
  <p>In line 500, why the key is assigned to a new variable <code>k</code> and then used in the OR condition? Why can't it be written directly as follows: </p>
  
  <pre><code>if (e.hash == hash &amp;&amp; (e.key == key || key.equals(e.key))) {
  </code></pre>
  
  <p><br>
  I don't know why it is written that way, But I am sure there is some reason behind why it is coded so. Is it some kind of optimization? <br>Can someone shed some light on this?</p>