title: java HashMap - how to retrieve both elements
body: <p>Consider a part of program:-</p>
  
  <pre><code>HashMap&lt;Object,Object&gt; m1=new HashMap&lt;Object,Object&gt;();
  Person p1=new Person("Raj","Sharma");
  Person p2=new Person("Raj","Sharma");
  m1.put(p1,"11");
  m1.put(p2,"22");
  </code></pre>
  
  <blockquote>
    <p>Also Person class have overridden hashCode() method but no equals() method</p>
  </blockquote>
  
  <p>What i know is: Since both have same keys (p1 and p2 produce same hashcode), and thus they correspond to same index, so after storing p1 in that index, when it try put p2 JVM see that both have same hashCode, so again hashing is done on p2 and new index is generated and 22 gets stored on that index. </p>
  
  <p>Now if we try to retrieve the element by using key p1 or p2,</p>
  
  <pre><code>String temp=(String)m1.get(p1);     // 11
  </code></pre>
  
  <p>it will return 11 only then how can we retrieve the element 22??</p>