title: Hash Set add method is not working fine
body: <p>I wrote a dummy program , that adds object in Hash Set. I created a class Car that has capacity of 5 people.</p>
  
  <p>Now issue is i got different out put from different Main programs .</p>
  
  <p>Kindly find the 2-Main programs below.</p>
  
  <p>First Main Program  is   </p>
  
  <pre><code>  public class Main_1 {
      static int counter = 0;
  
      public static void main(String args[]) {
  
          Car car = new Car();
          for (int i = 0; i &lt; 20; i++) {
              car.add(new Person());
          }
          car.done();
  
      }
  
       }
  </code></pre>
  
  <p><strong>The out put of Main_1 is : Exception in thread "main" java.lang.IllegalStateException: I'm full
   at Car.add(Car.java:10)
   at Main_1.main(Main_1.java:8)</strong></p>
  
  <p>Second Main program is </p>
  
  <pre><code>   public class Main_2 {
      static int counter = 0;
      static Car car = new Car();
  
      public static void main(String args[]) {
          car.add(new RecursivePerson());
          car.done();
      }
  
      static class RecursivePerson extends Person {
          public int hashCode() {
              if (++counter &lt; 20) {
                  car.add(new RecursivePerson());
              }
              return super.hashCode();
          }
      }
  }
  </code></pre>
  
  <p><strong>The out put of Main_2 is I'm a car with 20 people!</strong></p>
  
  <p>Below is the business logic of my program.     </p>
  
  <pre><code>      import java.util.HashSet;
        import java.util.Set;
  
          public class Car {
          private static final int CAPACITY = 5;
          private Set&lt;Person&gt; people = new HashSet&lt;Person&gt;();
  
          public synchronized void add(Person p) {
              if (people.size() &gt;= CAPACITY) {
                  throw new IllegalStateException("I'm full");
              } else {
                  people.add(p);
              }
          }
  
          public synchronized void done() {
              if (people.size() == 20) {
                  // The goal is to reach this line
                  System.out.println("I'm a car with 20 people!");
              }
          }
        }
  
          class Person {
           }
  </code></pre>
  
  <p>Can some one tell my why java is behaving like this.</p>