title: Simple HashSet contains misunderstanding
body: <p>I don't understand why HashSet returns false for next example. Code:</p>
  
  <pre><code>import java.util.*;
  
  public class Name {
      private String first, last;
  
      public Name(String first, String last){
          this.first = first; 
          this.last = last;
      }
  
      public boolean equals(Name n){
          return n.first.equals(first) &amp;&amp; n.last.equals(last);
      }
  
      public int hashCode(){
          return 31*first.hashCode()+last.hashCode();
      }
  
      public static void main(String[] args){
          Set&lt;Name&gt; s = new HashSet&lt;Name&gt;();
          Name n1 = new Name("Donald", "Duck");
          Name n2 = new Name("Donald", "Duck");
          s.add(n1);
  
          System.out.print("HashCodes equal: ");
          System.out.println( n1.hashCode() == n2.hashCode());
          System.out.print("Objects equal: ");
          System.out.println( n1.equals(n2) );
  
          System.out.print("HashSet contains n1: ");
          System.out.println(s.contains(n1));
  
          System.out.print("HashSet contains n2: ");
          System.out.println(s.contains(n2));
      }
  
  }
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>HashCodes equal: true
  Objects equal: true
  HashSet contains n1: true
  HashSet contains n2: false
  </code></pre>
  
  <p>Excerpt from <a href="http://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html#contains%28java.lang.Object%29" rel="nofollow">HashSet</a> contains method description:</p>
  
  <blockquote>
    <p>Returns true if this set contains the specified element. More
    formally, returns true if and only if this set contains an element e
    such that (o==null ? e==null : o.equals(e)).</p>
  </blockquote>
  
  <p>Question: why does it return false for both objects even though they are both not null and they are both equal in terms of hesh and value?</p>