Java Reference
In-Depth Information
The
hashCode
and
equals
methods are closely related because they rely on the same premise. They
add together a set of values to create a value that can be used for comparison (an equals-to comparison
in the case of the
equals
method and equals-to, greater-than, and less-than comparisons in the case of
the
hashCode
method).
■
Note
When you implement the
equals
method, you must also implement the
hashCode
method. The
hashCode
method determines where an object reference goes when put into a collection that uses a hashing
algorithm (such as the
HashMap
and
HashTable
classes). We address those two collections (and others) when we
get to data structures, later in the topic. The goal of the
hashCode
method is to return a unique (or at least nearly
unique) identifier; that's why it starts with a prime number and multiplies by another prime number. Again, we get
to that in much greater detail.
■
Caution
When you implement both
equals
and
hashCode
(and you should implement both if you implement
one), you must make sure that they use the same fields. For example, if you use
firstName
and
lastName
in the
equals
method, you must use
firstName
and
LastName
in the
hashCode
method. Otherwise, your comparisons
fail. Worse yet, you don't get exceptions; you get the wrong behavior and a hard-to-find bug.
■
Caution
The
hashCode
method must generate equal values for equal objects. Otherwise, comparisons that
should succeed fail, and you have another hard-to-find bug.
Comparisons for Sorting
I include sorting in this topic because it's impossible to sort things without comparing them. Suppose
you want to sort a bunch of colorful rocks into the spectrum. To do so, you pick up each rock, compare it
to the other rocks, and use that information to decide where in the row of rocks each rock belongs.
Sorting comes to most people pretty readily, with little training. However, a computer has to be told
exactly how to do it. To that end, Java offers two mechanisms for creating comparisons that can be used
for sorting: implementing the
compareTo
method from
java.util.Comparable
and creating a class that
extends
java.util.Comparator
. We do both for our
Person
class. Classes don't have to implement both,
but many do. The
String
class offers a fine example of a class that implements both comparison
interfaces (and
equals
and
hashCode
), by the way.
To be able to compare objects with the goal of sorting them, we have to know more than whether
one object equals another. We also have to know whether one object is greater than or less than another
object. For our color-sorting example, we can assign an integer value to each color and then sort our
rocks by putting each one to the left of all the rocks its color value is greater than and to the right of all
the rocks its color value is less than. Rocks with the same color value make piles of rocks whenever that
happens (and that's comparable to what happens in Java when hash code values are identical).