title: what is the difference between equals and contains methods
body: <p>I was working with the <code>TreeSet</code> collection with the following code:</p>
  
  <pre><code>import java.util.*;
  public class Employee implements Comparable&lt;Employee&gt;{
  
      private int ID;
  
      public Employee(int iD) {
          ID = iD;
      }
  
      @Override
      public int compareTo(Employee obj) {
          return this.ID-obj.ID;
      }
  
      private static void intoTreeSet() {
          Employee e1=new Employee(4);
          Employee e2=new Employee(2);
          Employee e3=new Employee(1);
          Employee e4=new Employee(5);
          Employee e5=new Employee(3);
  
          Employee eTemp=new Employee(3);
  
          Set&lt;Employee&gt; set=new TreeSet();
          set.add(e1);set.add(e2);set.add(e3);set.add(e4);set.add(e5);
  
          System.out.println("output says: ");
          for(Employee e:set){
              System.out.print(e.ID+" ~ ");
          }
          System.out.println();
          if(set.contains(eTemp)){
              System.out.println("C O N T A I N S !!!");
          }
  
          if(e5.equals(eTemp)){
              System.out.println("E Q U A L S !!!");
          }
      }
  
      public static void main(String[] args) {
          intoTreeSet();
      }
  }
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre><code>output says: 
  1 ~ 2 ~ 3 ~ 4 ~ 5 ~ 
  C O N T A I N S !!!
  </code></pre>
  
  <p>I am confused to see the output. I want to know, if it does NOT pass <code>equals</code> case, then how come it pass <code>contains</code> case.</p>
  
  <p>I know that two objects can only be equal if their class overrides <code>equals</code> method and they are equal according to some property. I intentionally did not override <code>equals</code> method to see how <code>contains</code> work. If it were a non-tree based collection lets say an <code>ArrayList</code> it would NOT have passed <code>contains</code> test. Why is it so ? can any one explain this behaviour and clear my confusion.</p>