What issues should be considered when overriding equals and hashCode in Java?(在 Java 中覆盖 equals 和 hashCode 时应该考虑哪些问题?)
问题描述
在覆盖 equals
和 hashCode
时必须考虑哪些问题/陷阱?
What issues / pitfalls must be considered when overriding equals
and hashCode
?
推荐答案
理论(语言律师和数学爱好者):
equals()
(javadoc) 必须定义一个等价关系(它必须是reflexive、symmetric和transitive).此外,它必须是一致的(如果对象没有被修改,那么它必须保持返回相同的值).此外,o.equals(null)
必须始终返回 false.
The theory (for the language lawyers and the mathematically inclined):
equals()
(javadoc) must define an equivalence relation (it must be reflexive, symmetric, and transitive). In addition, it must be consistent (if the objects are not modified, then it must keep returning the same value). Furthermore, o.equals(null)
must always return false.
hashCode()
(javadoc) 也必须是 consistent (如果对象没有根据 equals()
进行修改,它必须保持返回相同的值).
hashCode()
(javadoc) must also be consistent (if the object is not modified in terms of equals()
, it must keep returning the same value).
这两种方法之间的关系是:
只要 a.equals(b)
,则 a.hashCode()
必须与 b.hashCode()
相同.
Whenever
a.equals(b)
, thena.hashCode()
must be same asb.hashCode()
.
在实践中:
如果你覆盖了一个,那么你应该覆盖另一个.
In practice:
If you override one, then you should override the other.
使用与计算 equals()
相同的字段集来计算 hashCode()
.
Use the same set of fields that you use to compute equals()
to compute hashCode()
.
使用优秀的辅助类 EqualsBuilder 和 Apache Commons Lang 库中的 HashCodeBuilder.一个例子:
Use the excellent helper classes EqualsBuilder and HashCodeBuilder from the Apache Commons Lang library. An example:
public class Person {
private String name;
private int age;
// ...
@Override
public int hashCode() {
return new HashCodeBuilder(17, 31). // two randomly chosen prime numbers
// if deriving: appendSuper(super.hashCode()).
append(name).
append(age).
toHashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Person))
return false;
if (obj == this)
return true;
Person rhs = (Person) obj;
return new EqualsBuilder().
// if deriving: appendSuper(super.equals(obj)).
append(name, rhs.name).
append(age, rhs.age).
isEquals();
}
}
还要记住:
使用基于哈希的Collection 或 地图 例如HashSet, LinkedHashSet, HashMap, 哈希表,或 WeakHashMap,确保你放入集合中的关键对象的 hashCode() 在对象存在时永远不会改变集合.确保这一点的防弹方法是使您的密钥不可变,这还有其他好处.
Also remember:
When using a hash-based Collection or Map such as HashSet, LinkedHashSet, HashMap, Hashtable, or WeakHashMap, make sure that the hashCode() of the key objects that you put into the collection never changes while the object is in the collection. The bulletproof way to ensure this is to make your keys immutable, which has also other benefits.
这篇关于在 Java 中覆盖 equals 和 hashCode 时应该考虑哪些问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Java 中覆盖 equals 和 hashCode 时应该考虑哪些问题?


- Eclipse 的最佳 XML 编辑器 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 获取数字的最后一位 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 转换 ldap 日期 2022-01-01