title: Implement equals with Set
body: <p>I have this class:</p>
  
  <pre><code>private static class ClassA{
  int id;
  String name;
  
  public ClassA(int id, String name){
      this.id= id;
      this.name = name;
  }
  
  @Override
  public boolean equals(Object o) {
      return ((ClassA)o).name.equals(this.name);
  }   
  </code></pre>
  
  <p>}</p>
  
  <p>Why this main is printing 2 elements if I am overwriting the method equals in ClassA to compare only the name?</p>
  
  <pre><code>public static void main(String[] args){
      ClassA myObject = new ClassA(1, "testing 1 2 3");
      ClassA myObject2 = new ClassA(2, "testing 1 2 3");    
  
      Set&lt;ClassA&gt; set = new HashSet&lt;ClassA&gt;();
      set.add(myObject);
      set.add(myObject2);   
      System.out.println(set.size()); //will print 2, but I want to be 1!
  }
  </code></pre>
  
  <p>If I look into the Set Java <a href="http://download.oracle.com/javase/1.4.2/docs/api/java/util/Set.html" rel="nofollow">documentation</a>:</p>
  
  <p>A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.</p>
  
  <p>So apparently I only have to override equals, however I heard that I have also to override the hashcode, but why?</p>