Implementing hashCode()

Implementing hashCode()

Now we are discussing about the implementation of he hashcode(). hashcode () is simply use for the  comparison. we have a code for the hashCode().

class HasHash {
public int a;
HasHash(int xVal) {
a = aVal;
}
public boolean equals(Object o) {
HasHash h = (HasHash) o; // Don\'t try at home without
// instanceof test
if (h.a == this.a) {
return true;
} else {
return false;
}
}
public int hashCode() {
return (a * 22);
}
}


Because the equals() method considers two objects equal if they have the same a value, we have to be sure that objects with the same a value will return identical hashcodes.

By:umang
Date:2013-03-09 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related scjp1.5 chap8 Links

scjp1.5 chap8 interview questions and answers for experienced and fresher