title: Java comparing class instances
body: <p>I got this code:</p>
  
  <pre><code>public Class Car;
  {
      private String name;  
      public int number;     
  
  
      public Car(String name, int number) 
      {
          this.name = name;
          this.number = number;
      }
  
      // this class also got getters and setters 
  </code></pre>
  
  <p>Then I got another class:</p>
  
  <pre><code> public class CarList
      {
          private Collection&lt;Car&gt; cars;
  
  
          public CarList()
          {
              cars = new HashSet&lt;&gt;();
          }
  
  
         public boolean insertCar(Car car)
          {
              return cars.add(car); 
          }
  </code></pre>
  
  <p>Then code continues with some other methods but they are not problem.
  If I create two instances of class <code>Car</code>, for example <code>car1(mazda,1)</code> and <code>car2(porsche,2)</code> and then I call method <code>insertCar</code>, everything is OK, method returns <code>true</code> because there are two cars with different name and number.</p>
  
  <p>But if create for example <code>car1(mazda,1)</code> and then <code>car2(porsche,1)</code> method returns also <code>true</code> despite the fact that numbers of the cars are the same.</p>
  
  <p>EDIT:So my question is how to force method to return <code>false</code> when I try to ''add'' cars with the same number AND same name ( so how would @Override equals() method change )?</p>