Saturday 3 August 2013

32:equals() method and hashCode() method

Learning Objectives
After completing this session, you will be able to:
‰  Identify equals() method and hashCode() method
Java Object Law for equals() and hashCode() Methods
The Java API docs for the class Object state the following rules that a Java programmer must
adhere:
‰  If two objects are equal, then theymust have matching hashcodes.
‰  If two objects are equal, then calling equals() on the other object must return true. In
other words, if (a.equals(b)), then (b.equals(a)) .
‰  Java Object Law for equals() and hashCode() Methods (Contd.)
‰  If two objects have the same hashcode value, then they are not required to be equal.
But if they are equal, they must have the same hashcode value. So, if you override
equals() , you must override hashCode() .
‰  The default behavior of hashCode() is to generate a unique integer for each object on
the heap. So if you do not override hashCode() in a class, then no two objects of that
type can ever be considered equal.
‰  The default behavior of equals() is to do a == comparison. In other words, the default
behavior of equals() is to test whether the two references refer to a single object on
the heap. So if you do not override equals() in a class, then no two objects can ever be
considered equal because references to two different objects will always contain a
different bit pattern.
‰  a.equals(b) must also mean thata.hashCode() == b.hashCode().
‰  But, a.hashCode() == b.hashCode() does not have to mean a.equals(b).
Try It Out
Problem Statement:
Write a program that illustrates the usage of equals() and hashCode() methods.
Code:
class HashHash {
public int x;
public HashHash(int xVal) {
x = xVal;
}
public boolean equals(Object obj) {
HashHash h = (HashHash) obj;
if (h.x == this.x) {
return true;
} else {
return false;
}
}
public int hashCode() {
return (x * 17);
}
// continued …
Refer File Name: HashHash.javato obtain soft copy of the program code
How It Works:
‰  This program has overridden both the equals() and hashCode() methods.
‰  Notice that in order for an object to be located, the search object and the object in the
collection must have both identical hashcodevalues and return true for the equals()
method.
‰  So there is just no way out of overridingboth methods to be absolutely certain that
your objects can be applied in Collections that implement hashing.
Tips and Tricks:
How come hashcodes can be the same even if objects are not equal?
Solution:
‰  HashSets use hashcodes to store the elements in a way that makes it much faster to
access. If you try to find an object in an ArrayList by giving the ArrayList a copy of the
object (as opposed to an index value), then the ArrayList has to start searching from
the beginning, looking at each element in the list to see if it matches. But a HashSet
can find an object more quickly, because it uses the hashcode as a kind of label on
the “bucket” where it stored the element.
‰  Hashcodes can be the same without necessarily guaranteeing that the objects are
equal, because the “hashing algorithm” applied in the hashCode() method might
happen to return the same value for multiple Objects. This means that multiple objects
would all land in the same bucket in the HashSet.
‰  Hashcode values are sometimes used to narrow down the search, but to find the one
exact match, the HashSet still has to take all the objects in that one bucket (the bucket
for all objects with the same hashcode) and then call equals() on them to see if the
object it is looking for is in that bucket.
Summary
‰  equals(), hashCode(), and toString() are public.
‰  When overridding equals(), use the instanceof operator to be sure you are evaluating
an appropriate class.
‰  Highlights of the equals() contract:
oReflexive: x.equals(x) is true.
oSymmetric: if x.equals(y) is true, then y.equals(x) must be true.
oTransitive: if x.equals(y) is true, and y.equals(z) is true, then z.equals(x) is true.
oConsistent: Multiple calls to x.equals(y) will return the same result.
oNull: If x.equals(y) is true, then x.hashCode() == y.hashCode() is true.
oIf you override equals(),override hashCode().
‰  Highlights of the hashCode() contract:
oConsistent: multiple calls to x.hashCode() return the same integer.
oIf x.equals(y) is true, x.hashCode() == y.hashCode() is true.
oIf x.equals(y) is false, then x.hashcode() == y.hashCode() can be either true or
false, but false will tend to create better efficiency.
otransient variables are not appropriate for equals() and hashCode().
Test Your Understanding
1.State true or false for the following:
a)If two objects are equal, then they must have matching hashcodes.
b)a.equals(b) must also mean that a.hashCode() == b.hashCode()
c)a.hashCode() == b.hashCode() must also mean that a.equals(b)

No comments:

Post a Comment