在Java中重写equals和hashCode时应该考虑哪些问题?
重写 equals 和 hashCode 时必须考虑哪些问题/陷阱?
What issues / pitfalls must be considered when overriding equals
and hashCode
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我发现的一个问题是两个对象包含彼此的引用(一个例子是父/子关系,在父对象上使用一种方便的方法来获取所有子对象)。
例如,在进行 Hibernate 映射时,此类事情相当常见。
如果您在 hashCode 或 equals 测试中包含关系的两端,则可能会进入以 StackOverflowException 结束的递归循环。
最简单的解决方案是不在方法中包含 getChildren 集合。
One gotcha I have found is where two objects contain references to each other (one example being a parent/child relationship with a convenience method on the parent to get all children).
These sorts of things are fairly common when doing Hibernate mappings for example.
If you include both ends of the relationship in your hashCode or equals tests it's possible to get into a recursive loop which ends in a StackOverflowException.
The simplest solution is to not include the getChildren collection in the methods.
逻辑上我们有:
a.getClass().equals(b.getClass()) && a.equals(b) ⇒
a.hashCode() == b.hashCode()
但不反之亦然!
Logically we have:
a.getClass().equals(b.getClass()) && a.equals(b)
⇒a.hashCode() == b.hashCode()
But not vice-versa!
仍然令人惊讶的是没有人为此推荐番石榴库。
Still amazed that none recommended the guava library for this.
超类 java.lang.Object 中有两个方法。 我们需要将它们重写为自定义对象。
只要它们相等,相等的对象就必须产生相同的哈希码,但是不相等的对象不需要产生不同的哈希码。
如果您想了解更多信息,请检查此链接 http://www.javaranch。 com/journal/2002/10/equalhash.html
这是另一个例子,
http://java67.blogspot .com/2013/04/example-of-overriding-equals-hashcode-compareTo-java-method.html
玩得开心! @.@
There are two methods in super class as java.lang.Object. We need to override them to custom object.
Equal objects must produce the same hash code as long as they are equal, however unequal objects need not produce distinct hash codes.
If you want get more, please check this link as http://www.javaranch.com/journal/2002/10/equalhash.html
This is another example,
http://java67.blogspot.com/2013/04/example-of-overriding-equals-hashcode-compareTo-java-method.html
Have Fun! @.@
在检查成员相等性之前,有几种方法可以检查类相等性,我认为这两种方法在适当的情况下都很有用。
instanceof
运算符。我在
final
equals 实现中使用#1,或者在实现规定 equals 算法的接口时使用#1(例如java.util
集合接口 - 检查的正确方法)与(obj instanceof Set)
或您正在实现的任何接口)。 当 equals 可以被覆盖时,这通常是一个糟糕的选择,因为这会破坏对称性。选项 #2 允许安全地扩展类,而不会覆盖 equals 或破坏对称性。
如果您的类也是
Comparable
,则equals
和compareTo
方法也应该一致。 以下是Comparable
类中 equals 方法的模板:There are a couple of ways to do your check for class equality before checking member equality, and I think both are useful in the right circumstances.
instanceof
operator.this.getClass().equals(that.getClass())
.I use #1 in a
final
equals implementation, or when implementing an interface that prescribes an algorithm for equals (like thejava.util
collection interfaces—the right way to check with with(obj instanceof Set)
or whatever interface you're implementing). It's generally a bad choice when equals can be overridden because that breaks the symmetry property.Option #2 allows the class to be safely extended without overriding equals or breaking symmetry.
If your class is also
Comparable
, theequals
andcompareTo
methods should be consistent too. Here's a template for the equals method in aComparable
class:对于平等,请查看平等的秘密作者:安吉莉卡·兰格。 我非常爱它。 她还是一个关于Java 泛型的精彩常见问题解答。 此处查看她的其他文章(向下滚动到“Core Java”),她在其中还继续第 2 部分和“混合类型比较”。 祝您阅读愉快!
For equals, look into Secrets of Equals by Angelika Langer. I love it very much. She's also a great FAQ about Generics in Java. View her other articles here (scroll down to "Core Java"), where she also goes on with Part-2 and "mixed type comparison". Have fun reading them!
equals()方法用于判断两个对象是否相等。
因为 int 值 10 始终等于 10。但是这个 equals() 方法是关于两个对象相等的。 当我们说对象时,它就会有属性。 为了决定平等,需要考虑这些属性。 不必考虑所有属性来确定相等性,并且可以根据类定义和上下文来决定。 然后可以重写 equals() 方法。
每当我们重写 equals() 方法时,我们都应该重写 hashCode() 方法。 如果没有,会发生什么? 如果我们在应用程序中使用哈希表,它将不会按预期运行。 由于 hashCode 用于确定存储的值的相等性,因此它不会返回键的正确对应值。
给出的默认实现是 Object 类中的 hashCode() 方法,使用对象的内部地址并将其转换为整数并返回。
示例代码输出:
equals() method is used to determine the equality of two objects.
as int value of 10 is always equal to 10. But this equals() method is about equality of two objects. When we say object, it will have properties. To decide about equality those properties are considered. It is not necessary that all properties must be taken into account to determine the equality and with respect to the class definition and context it can be decided. Then the equals() method can be overridden.
we should always override hashCode() method whenever we override equals() method. If not, what will happen? If we use hashtables in our application, it will not behave as expected. As the hashCode is used in determining the equality of values stored, it will not return the right corresponding value for a key.
Default implementation given is hashCode() method in Object class uses the internal address of the object and converts it into integer and returns it.
Example Code Output:
关于 obj.getClass() != getClass() 的说明。
该语句是
equals()
继承不友好的结果。 JLS(Java 语言规范)规定,如果A.equals(B) == true
则B.equals(A)
也必须返回true
。 如果您忽略该语句,则继承重写 equals() 的类(并更改其行为)将违反此规范。考虑以下示例,说明省略该语句时会发生什么:
执行
new A(1).equals(new A(1))
另外,new B(1,1).equals( new B(1,1))
结果给出 true,正如它应该的那样。这看起来非常好,但是看看如果我们尝试使用这两个类会发生什么:
显然,这是错误的。
如果要保证对称条件。 a=b 如果 b=a 并且里氏替换原则不仅在
B
实例的情况下调用super.equals(other)
,而且在之后检查A
实例:将输出:
其中,如果
a
不是B
的引用,那么它可能是类A< 的引用/code> (因为您扩展了它),在这种情况下您调用
super.equals()
too。A clarification about the
obj.getClass() != getClass()
.This statement is the result of
equals()
being inheritance unfriendly. The JLS (Java language specification) specifies that ifA.equals(B) == true
thenB.equals(A)
must also returntrue
. If you omit that statement inheriting classes that overrideequals()
(and change its behavior) will break this specification.Consider the following example of what happens when the statement is omitted:
Doing
new A(1).equals(new A(1))
Also,new B(1,1).equals(new B(1,1))
result give out true, as it should.This looks all very good, but look what happens if we try to use both classes:
Obviously, this is wrong.
If you want to ensure the symmetric condition. a=b if b=a and the Liskov substitution principle call
super.equals(other)
not only in the case ofB
instance, but check after forA
instance:Which will output:
Where, if
a
is not a reference ofB
, then it might be a be a reference of classA
(because you extend it), in this case you callsuper.equals()
too.如果您正在处理使用像 Hibernate 这样的对象关系映射器 (ORM) 持久化的类,并且您不认为这已经变得不合理的复杂,那么有一些问题值得注意!
延迟加载的对象是子类
如果您的对象使用 ORM 进行持久化,则在许多情况下您将处理动态代理以避免过早从数据存储中加载对象。 这些代理作为您自己的类的子类来实现。 这意味着
this.getClass() == o.getClass()
将返回false
。 例如:如果您正在处理 ORM,则使用
o instanceof Person
是唯一能够正确运行的方法。延迟加载的对象具有空字段< ORM
通常使用 getter 来强制加载延迟加载的对象。 这意味着如果
person
延迟加载,person.name
将为null
,即使person.getName()
强制加载并返回“John Doe”。 根据我的经验,这种情况在hashCode()
和equals()
中更常见。如果您正在处理 ORM,请确保始终使用 getter,并且切勿在
hashCode()
和equals()
中使用字段引用。保存对象将改变其状态
持久对象通常使用
id
字段来保存对象的键。 首次保存对象时,此字段将自动更新。 不要在hashCode()
中使用 id 字段。 但您可以在equals()
中使用它。我经常使用的模式是
但是:您不能在
hashCode()
中包含getId()
。 如果这样做,当对象被持久化时,它的hashCode
会发生变化。 如果该对象位于HashSet
中,您将“永远”不会再找到它。在我的
Person
示例中,我可能会使用getName()
来表示hashCode
和getId()
以及getName()
(只是出于偏执)用于equals()
。 如果hashCode()
存在一些“冲突”风险,那是可以的,但对于equals()
则绝对不行。hashCode()
应使用equals()
中不变的属性子集There are some issues worth noticing if you're dealing with classes that are persisted using an Object-Relationship Mapper (ORM) like Hibernate, if you didn't think this was unreasonably complicated already!
Lazy loaded objects are subclasses
If your objects are persisted using an ORM, in many cases you will be dealing with dynamic proxies to avoid loading object too early from the data store. These proxies are implemented as subclasses of your own class. This means that
this.getClass() == o.getClass()
will returnfalse
. For example:If you're dealing with an ORM, using
o instanceof Person
is the only thing that will behave correctly.Lazy loaded objects have null-fields
ORMs usually use the getters to force loading of lazy loaded objects. This means that
person.name
will benull
ifperson
is lazy loaded, even ifperson.getName()
forces loading and returns "John Doe". In my experience, this crops up more often inhashCode()
andequals()
.If you're dealing with an ORM, make sure to always use getters, and never field references in
hashCode()
andequals()
.Saving an object will change its state
Persistent objects often use a
id
field to hold the key of the object. This field will be automatically updated when an object is first saved. Don't use an id field inhashCode()
. But you can use it inequals()
.A pattern I often use is
But: you cannot include
getId()
inhashCode()
. If you do, when an object is persisted, itshashCode
changes. If the object is in aHashSet
, you'll "never" find it again.In my
Person
example, I probably would usegetName()
forhashCode
andgetId()
plusgetName()
(just for paranoia) forequals()
. It's okay if there are some risk of "collisions" forhashCode()
, but never okay forequals()
.hashCode()
should use the non-changing subset of properties fromequals()
对于继承友好的实现,请查看 Tal Cohen 的解决方案,如何正确实现 equals() 方法?
摘要:
在他的书中 有效的 Java 编程语言指南(Addison-Wesley,2001 年),Joshua Bloch 声称“根本没有办法在保留 equals 契约的同时扩展可实例化类并添加方面。” 塔尔不同意。
他的解决方案是通过双向调用另一个非对称的blindlyEquals()来实现equals()。 blindlyEquals() 被子类重写,equals() 被继承,并且永远不会被重写。
示例:
请注意,如果要满足 里氏替换原则,则 equals() 必须跨继承层次结构工作。
For an inheritance-friendly implementation, check out Tal Cohen's solution, How Do I Correctly Implement the equals() Method?
Summary:
In his book Effective Java Programming Language Guide (Addison-Wesley, 2001), Joshua Bloch claims that "There is simply no way to extend an instantiable class and add an aspect while preserving the equals contract." Tal disagrees.
His solution is to implement equals() by calling another nonsymmetric blindlyEquals() both ways. blindlyEquals() is overridden by subclasses, equals() is inherited, and never overridden.
Example:
Note that equals() must work across inheritance hierarchies if the Liskov Substitution Principle is to be satisfied.
理论(对于语言律师和数学爱好者):
equals()
(javadoc) 必须定义一个等价关系(它必须是自反、对称 和传递)。 此外,它必须一致(如果对象没有被修改,那么它必须保持返回相同的值)。 此外,o.equals(null)
必须始终返回 false。hashCode()
(javadoc) 也必须一致(如果对象没有根据equals()
进行修改,则它必须保持返回相同的值)。两种方法之间的关系是:
实际上:
如果您覆盖其中一个,那么您应该覆盖另一个。
使用与计算
equals()
相同的字段集来计算hashCode()
。使用优秀的帮助器类 EqualsBuilder< /a> 和 HashCodeBuilder 来自 Apache Commons Lang 库。 一个例子:
还请记住:
当使用基于哈希的 集合或地图 例如HashSet,LinkedHashSet, HashMap, 哈希表,或 WeakHashMap,确保放入集合的关键对象的 hashCode() 在对象在集合中。 确保这一点的万无一失的方法是使您的密钥不可变,这还有其他好处< /a>.
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) must also be consistent (if the object is not modified in terms ofequals()
, it must keep returning the same value).The relation between the two methods is:
In practice:
If you override one, then you should override the other.
Use the same set of fields that you use to compute
equals()
to computehashCode()
.Use the excellent helper classes EqualsBuilder and HashCodeBuilder from the Apache Commons Lang library. An example:
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.