title: Java Set gets duplicate entry
body: <p>JavaDoc defines set as : </p>
  
  <blockquote>
    <p>A collection that contains no duplicate elements. More formally, sets
    contain no pair of elements e1 and e2 such that e1.equals(e2)</p>
  </blockquote>
  
  <p>To verify the same, i created a very simple program:</p>
  
  <pre><code>import java.util.HashSet;
  
  public class CheckHashSet {
      public static void main(String[] args) {
          HashSet&lt;Employee&gt; set = new HashSet&lt;Employee&gt;();
          set.add(new Employee(10));
          set.add(new Employee(10));
          System.out.println(set.size());
          System.out.println(new Employee(10).equals(new Employee(10)));
      }
  
      private static class Employee implements Comparable&lt;Employee&gt; {
          private final int id;
          public Employee(int id) {
              this.id = id;
          }
          @Override
          public int compareTo(Employee o) {
              return this.id - o.id; 
          }
  
          @Override
          public boolean equals(Object obj) {
              if(obj instanceof Employee) {
                  return compareTo((Employee)obj)==0;
              }
              return false;
          }
      }
  }
  </code></pre>
  
  <p>The output of the program is </p>
  
  <pre><code>2
  true
  </code></pre>
  
  <p>This means <code>new Employee(10).equals(new Employee(10))</code> returns true whereas <code>set.add(new Employee(10)); set.add(new Employee(10));</code> adds the object twice.</p>
  
  <p>What is wrong with my code?</p>