title: Overridden equals and hashCode does not work on custom Object;
body: <p>I have an below object</p>
  
  <pre><code>class CustomObj{
      private String name;
      private String dept;
  
     public String getName(){
       return this.name;
     }
  
     public String getDept(){
       return this.dept;
     }
  
     private CustomObj(){
     }
  
     private CustomObj(CustomObjBuilder builder){
       this.name = builder.name;
       this.dept= builder.dept;
    }
  
     @Override
     public boolean equals(Object o) {
  
        if (o == this) return true;
        if (o == null || getClass() != o.getClass()) return false;
  
        CustomObj that = (CustomObj) o;
  
        return that.name.equals(name) &amp;&amp;
              that.dept.equals(dept);
      }
  
     @Override
     public int hashCode() {
       int result = 31;
       result = 31 * result + name.hashCode();
       result = 31 * result + dept.hashCode();
       return result;
     }
  
    public static class CustomObjBuilder{
       private String name;
       private  String dept;
  
  
       public CustomObjBuilder(String name, String dept){
          this.name = name;
          this.dept = dept;
       }
  
       public CustomObjBuilder setName(String name){
         this.name = name;
         return this;
       }
  
       public CustomObjBuilder setDept(String dept){
        this.dept = dept;
        return this;
       }
  
       public CustomObj build(){
              return new CustomObj(this);
      }
  }
   }
  </code></pre>
  
  <p>and class that uses above</p>
  
  <pre><code>class XYZ{
     Set&lt;CustomObj&gt; obj = new HashSet&lt;CustomObj&gt;();
  
     public void process(String a, String b){
         CustomObj o = new CustomObj.CustomObjBuilder(a,b).build();
         if(!obj.contains(o)){
          obj.add(o);
         } 
     }
  }
  </code></pre>
  
  <p>And a test class</p>
  
  <pre><code>class TestXYX{
  
  @Test
  public void test(){
   XYZ xyz = new XYZ();
   xyz.process("TEST","TESTABC");
   xyz.process("TEST","TESTABC");
  }
  }
  </code></pre>
  
  <p>Beacuse I have overrideen hascode and equals, both the above are equal and when process is called second time, the control should not go into <code>if(!obj.contains(o))</code> second time and size of the set should be 1. But when i run the test  <code>obj.add(o);</code> is called two times. But the values of both this object and that objec inside equals methods are same, but </p>
  
  <pre><code>that.name.equals(name) &amp;&amp; that.dept.equals(dept)
  </code></pre>
  
  <p>inside CustomObj returns false. Can someone please help me understand why?</p>