When implementing the
hashCode
method we should make sure that it's consistent
and always return the same value for objects for which the equals method return true.
The
hashCode
method is defined in the
java.lang.Object
class and returns the object internal address:
The
java.util.Objects
class provides utility methods to get the hash code of an object or multiple objects:
The
java.util.Arrays
class provides utility methods to get the hash code of an array of objects
(including specific overloaded methods for each primitive type).
It also provides a utility method to get a deep hash code of an array of objects (array of arrays).
The following is a sample code that show how to implement the
hashCode
method.
The code provide an implementation of the
hashCode
method
using
hashCode
and
hash
methods
of the
java.util.Objects
class.
-
Superclass "P":
-
Subclass "C":