title: Why hash set allows adding duplicate object?
body: <pre><code>import java.util.Enumeration;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.Vector;
  
  public class Test {
  
      public static void main(String[] args) {
  
          Employee e1 = new Employee("abc",10.0);
          Employee e3 = new Employee("abc",10.0);
  
          HashSet&lt;Employee&gt; hs = new HashSet&lt;Employee&gt;();
          hs.add(e1);
          hs.add(e3);
  
          System.out.println("size of hs : "+hs.size());
  
          Object [] aa = hs.toArray();
  
          for(int i=0;i&lt;aa.length;i++){
  
              Object ii = aa[i];
              System.out.println("ii "+(i+1)+"="+ii.toString());
          }
  
          Iterator it = hs.iterator();
  
          while(it.hasNext()){
              Employee e4 = (Employee) it.next();
              System.out.println("e4 ="+e4);
              System.out.println("111="+it.next());
          }
  
          Enumeration e5 = new Vector(hs).elements();
          while(e5.hasMoreElements()){
              Employee e6 = (Employee) e5.nextElement();
              System.out.println("e6 ="+e6);
          }
  
      }
  
  }
  
  public class Employee {
  
      private String name;
      private Double salary;
  
      public Employee(String name, Double salary){
          this.name = name;
          this.salary = salary;
      }
  
      public Employee(String name){
          this.name = name;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public Double getSalary() {
          return salary;
      }
  
      public void setSalary(Double salary) {
          this.salary = salary;
      }
  
      @Override
      public String toString() {
          return "Employee [name=" + name + ", salary=" + salary + "]";
      }
  
      public void getNameSal() throws NullPointerException{
          System.out.println(this.name +""+this.salary);
      }
  
  }
  </code></pre>
  
  <p>looking at above code, I have created one hash set which accepts object of <code>Employee</code> class.
  I have created two objects of <code>Employee</code> class with same values and added in hash set.
  But when I print size of hash set it shows 2.
  And also when iterate by three ways by converting it into array, <code>Iterator</code> and <code>Enumerator</code> then it displays both the values which are duplicate.
  But when I try to print using <code>it.next()</code> it prints only single value.
  Why is it so?</p>
  
  <pre><code>Output:
  size of hs : 2
  ii 1=Employee [name=abc, salary=10.0]
  ii 2=Employee [name=abc, salary=10.0]
  e4 =Employee [name=abc, salary=10.0]
  111=Employee [name=abc, salary=10.0]
  e6 =Employee [name=abc, salary=10.0]
  e6 =Employee [name=abc, salary=10.0]
  </code></pre>