java 我什么时候需要覆盖 equals 和 hashcode 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13134050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When do I need to override equals and hashcode methods?
提问by user926958
Possible Duplicate:
Overriding equals and hashCode in Java
If I have
如果我有
class A {
int x = 1;
}
...
A a1 = new A();
A a2 = new A();
a1.equals(a2);
If I compare 2 instances of A without override the equals method, will I get expected result?
如果我比较 A 的 2 个实例而不覆盖 equals 方法,我会得到预期的结果吗?
回答by Jon Skeet
If I compare 2 instances of A without override the equals method, will I get expected result?
如果我比较 A 的 2 个实例而不覆盖 equals 方法,我会得到预期的结果吗?
That depends on what you expect :)
这取决于你的期望:)
The default implementation will give you reference equality- in other words, when you compare two references, equals
will only return true if they're references to the same object.
默认实现将为您提供引用相等性- 换句话说,当您比较两个引用时,equals
如果它们是对同一对象的引用,则只会返回 true。
You would normally override equals
to implement "value equality" - where two distinct objects are deemed equal, usually by virtue of having equal field values themselves. The exact meaning of equality will depend on your design - the two objects could still be distinguishable in other ways, for example.
您通常会覆盖equals
以实现“值相等”——其中两个不同的对象被视为相等,通常是因为它们本身具有相等的字段值。相等的确切含义取决于您的设计 - 例如,这两个对象仍然可以通过其他方式区分。
If you override equals
, you should alsooverride hashCode
to be consistent with equals
, such that if a.equals(b)
is true, then a.hashCode() == b.hashCode()
. This will allow instances of your class to be used as keys in hash-based collections (e.g. HashMap
) so that you can look up a value based on a key which is equalto the original one, rather than having to use a reference to the exact original key object.
如果您覆盖equals
,您还应该覆盖hashCode
以与 一致equals
,这样如果a.equals(b)
为真,则a.hashCode() == b.hashCode()
。这将允许您的类的实例用作基于哈希的集合中的键(例如HashMap
),以便您可以根据与原始键相同的键查找值,而不必使用对确切原始密钥对象。
回答by Amit Sharma
If I compare 2 instances of A without override the equals method, will I get expected result?
如果我比较 A 的 2 个实例而不覆盖 equals 方法,我会得到预期的结果吗?
No. Since you have created two different instances explicitly.
否。因为您已明确创建了两个不同的实例。
Why?The default implementation of equals checks whether the two concerned objects point to the same memory location in java virtual memory (and this default behavior is defined in java.lang.Object.equals())
为什么?equals 的默认实现检查两个相关对象是否指向 java 虚拟内存中的相同内存位置(这个默认行为在 java.lang.Object.equals() 中定义)
When do I need to override equals and hashcode methods?
我什么时候需要覆盖 equals 和 hashcode 方法?
The most common scenario when programmers override both equals() and hashcode() are if you need to use instances of the concerned class as
程序员覆盖 equals() 和 hashcode() 时最常见的情况是,如果您需要使用相关类的实例作为
- Keys in java.util.Map implementations
- Values in java.util.Set implementations
- You want to check equality of values between two different instances of the same class (overriding equals() is mandatory in this case, hashcode() is not - but is a good programming practice to)
- java.util.Map 实现中的键
- java.util.Set 实现中的值
- 您想检查同一类的两个不同实例之间值的相等性(在这种情况下,必须覆盖 equals(),hashcode() 不是 - 但这是一个很好的编程实践)
General contract of equals and hashcode is :
equals 和 hashcode 的一般契约是:
if a1.equals(a2)
it is mandatory that a1.hashcode() == a2.hashcode()
if a1.hashcode() == a2.hashcode()
it is not mandatory that a1.equals(a2)
I guess that enough data to process for a day :)
我想足够处理一天的数据:)
回答by Ted Hopp
The default implementation of equals
tests whether the variables reference the same object. If that's not what you want, then you need to override equals
. When you override equals
you generally need to override hashcode
for the object to be usable in hash tables (or other data structures that make use of hash codes).
equals
测试变量是否引用同一个对象的默认实现。如果这不是您想要的,那么您需要覆盖equals
. 当您覆盖时,equals
您通常需要覆盖hashcode
对象才能在哈希表(或其他使用哈希码的数据结构)中使用。