If we implement our own keys in Hashtable, then our custom hashtable keys must implement
public int hashCode()
{
}
and
public Object equals(Object obj)
{
}
What will be the implementations for these methods?
If we implement our own keys in Hashtable, then our custom hashtable keys must implement
public int hashCode()
{
}
and
public Object equals(Object obj)
{
}
What will be the implementations for these methods?
Read this article: http://www.ibm.com/developerworks/java/library/j-jtp05273.html
Read "Effective Java 2nd Edition", this is a good time for it.
HashCode and Equals method in Java object – A pragmatic concept
Effective Java 2nd edition has the best explaination for these two methods: check the gory deailt here.
After you understood it by reading effective java, you might use commons lang EqualsBuilder and HashCodeBuilder to implement it. If the part isn't performance critical, you even can use the refelction method like this:
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj);
}
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
It don't gets much easier :)
These methods are used for hashtable implementation to identify elements while inserting and retrieval.