1

How would you write an equals() method? I need to be able to write a useable one for comparing playing cards in the program. I am using NetBeans for the writting of the code.

I also tend to notice that equals() methods often come with hashCode() methods. What is hashCode meant to do exactly and how should they be written?

So, how should I write an a by-the-book equals() method and a hashCode() method if I need it?


I will post the two equals() methods I ended up doing yesterday, and if anyone needs additional infomation on my program in particular, let me know and I will add the rest.

This is my current set up, unfortantely it will always printout the same output (false).

@Override
public boolean equals(Object otherObject)
    {
       boolean set = false;
       if (!(otherObject instanceof PlayingCard))
       {
          set = false;
       }

       if (otherObject == this)
       {
          set = true;
       }
       System.out.println(set);
       return set;
    }

This is (I think) the original equals() method I used.

@Override
public boolean equals(Object otherObject)
{
if (otherObject == null)
{
    System.out.println("Match");
    return false;
}
if (getClass() != otherObject.getClass())
{
    System.out.println("Match");
    return false;
}

System.out.println("No Match, True");
PlayingCard other = (PlayingCard) otherObject;
return suit.equals(other.suit) && rank == other.rank;
}
SIHB007
  • 69
  • 1
  • 10

1 Answers1

1

Your equals method should compare properties of the objects that determine equality.

Therefore, the second version makes more sense than the first (since the first only tests for reference equality, which is already done in the default implementation in Object class).

You can have a cleaner implementation though :

@Override
public boolean equals(Object otherObject)
{
    if (otherObject == null)
    {
        return false;
    }
    if (!(otherObject instanceof PlayingCard))
    {
        return false;
    }
    if (this == otherObject) {
        return true;
    }
    PlayingCard other = (PlayingCard) otherObject;
    return suit.equals(other.suit) && rank == other.rank;
}

hashCode is used by data structures that require a hashing function (HashSet, HashMap, etc...). It determines where an element will be stored in such data structures, and therefore, if two objects are equal, they must have the same hashCode.

In other words, your hashCode implementation should match the equals implementation, such that if a.equals(b) then a.hashCode() == b.hashCode(). Therefore, in your example, hashCode should be a function of the suit and rank properties.

For example :

@Override
public int hashCode ()
{
    return Objects.hash(suit,rank);
}
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Does Objects.hash come from the Java SE API, or from an external library? – nanofarad Feb 21 '15 at 14:30
  • I have just put in the code here to try it, but I am getting error notifications through netbeans. For a start, it cannot find these `other` and `Objects` symbols/variables, and that `!otherObject` is bad for unary operators, specificaly `!`. – SIHB007 Feb 21 '15 at 14:38
  • @SIHB007 You should import `java.util.Objects` (assuming you are compiling your code with Java 7 or higher). !otherObject is a typo, I forgot parentheses. other is also a typo - I'll edit the answer. – Eran Feb 21 '15 at 14:51
  • @SIHB007 Fixed the typos. – Eran Feb 21 '15 at 14:52
  • @hexafraction java.util.Objects is in Java SE API since Java 7. – Eran Feb 21 '15 at 14:53
  • Thanks, I was not aware of its presence. – nanofarad Feb 21 '15 at 14:53
  • Just one final question, how will I be able to test and find out if two cards are equal or not through the method? – SIHB007 Feb 21 '15 at 15:10
  • @SIHB007 `if (card1.equals(card2)) ...` – Eran Feb 21 '15 at 15:11
  • Thats what I do in the tester (`test.equals(test2)`) but all that happens is that the program just runs and I don't actually know if they are equal or not, it doesnt let me know. As in a statement. – SIHB007 Feb 21 '15 at 15:19
  • @SIHB007 You can print something - `if (card1.equals(card2)) {System.out.println("equal");} else {System.out.println("not equal");}` – Eran Feb 21 '15 at 15:24
  • Alright, I have sorted it out, thanks. – SIHB007 Feb 21 '15 at 15:36