title: Comparing two objects in Java .. each object has two fields
body: <p>I want to compare between two cards in poker hand .. each of them has a suit and a rank and I want to check if there are duplicates to create an exception .. need help with this piece of code please ! is it ok to use the equals method or it is wrong ?! </p>
  
  <pre><code>import java.util.ArrayList;
  
  public class Pokerhand {
  
      public final int CARDS_NUMBER = 5;
      ArrayList&lt;Card&gt; cards = new ArrayList&lt;Card&gt;();
  
      public Pokerhand (Card card1 , Card card2, Card card3, Card card4, Card card5)
      {
          cards.add(card1);
          cards.add(card2);
          cards.add(card3);
          cards.add(card4);
          cards.add(card5);
      }
  
      private boolean check(ArrayList&lt;Card&gt; cards)
      {
          if (cards.size() != CARDS_NUMBER)
              throw new IllegalArgumentException("Incorrect number of cards!! ");
  
          for ( int i=0 ; i&lt;= cards.size(); i++)
          {
              if (cards.get(i).equals(cards.get(i+1)))
                  throw new IllegalArgumentException("Duplicat card");
          }
      }
  }
  </code></pre>