If we implement our own keys in Hashtable, then our custom hashtable keys must implement
如果我们在Hashtable中实现自己的键,那么我们的自定义哈希表键必须实现
public int hashCode()
{
}
and
public Object equals(Object obj)
{
}
What will be the implementations for these methods?
这些方法的实现是什么?
5 个解决方案
#1
Read "Effective Java 2nd Edition", this is a good time for it.
阅读“Effective Java 2nd Edition”,这是一个很好的时机。
HashCode and Equals method in Java object – A pragmatic concept
Java对象中的HashCode和Equals方法 - 一个实用的概念
Java等于实现
Equals and Hashcode Implementation.
等于和Hashcode实现。
#2
Effective Java 2nd edition has the best explaination for these two methods: check the gory deailt here.
有效的Java第二版对这两种方法有最好的解释:在这里查看血淋淋的副本。
#3
Read this article: http://www.ibm.com/developerworks/java/library/j-jtp05273.html
阅读这篇文章:http://www.ibm.com/developerworks/java/library/j-jtp05273.html
#4
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:
通过阅读有效的java来理解它之后,你可以使用commons lang EqualsBuilder和HashCodeBuilder来实现它。如果零件不是性能关键,你甚至可以像这样使用refelction方法:
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj);
}
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
It don't gets much easier :)
它没有变得容易:)
#5
These methods are used for hashtable implementation to identify elements while inserting and retrieval.
这些方法用于哈希表实现,以在插入和检索时识别元素。
- hashcode is the key for storing
- equals used methods contains, get etc to check the keys.
hashcode是存储的关键
equals使用的方法包含,get等来检查密钥。
#1
Read "Effective Java 2nd Edition", this is a good time for it.
阅读“Effective Java 2nd Edition”,这是一个很好的时机。
HashCode and Equals method in Java object – A pragmatic concept
Java对象中的HashCode和Equals方法 - 一个实用的概念
Java等于实现
Equals and Hashcode Implementation.
等于和Hashcode实现。
#2
Effective Java 2nd edition has the best explaination for these two methods: check the gory deailt here.
有效的Java第二版对这两种方法有最好的解释:在这里查看血淋淋的副本。
#3
Read this article: http://www.ibm.com/developerworks/java/library/j-jtp05273.html
阅读这篇文章:http://www.ibm.com/developerworks/java/library/j-jtp05273.html
#4
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:
通过阅读有效的java来理解它之后,你可以使用commons lang EqualsBuilder和HashCodeBuilder来实现它。如果零件不是性能关键,你甚至可以像这样使用refelction方法:
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj);
}
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
It don't gets much easier :)
它没有变得容易:)
#5
These methods are used for hashtable implementation to identify elements while inserting and retrieval.
这些方法用于哈希表实现,以在插入和检索时识别元素。
- hashcode is the key for storing
- equals used methods contains, get etc to check the keys.
hashcode是存储的关键
equals使用的方法包含,get等来检查密钥。