Java:自动 equals() 和 hashCode()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6959307/
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
Java: Automatic equals() and hashCode()
提问by Ondra ?i?ka
Implementing equals()
and hashCode()
for simple data POJOs is cluttering my code and maintaining is tedious.
实施equals()
和hashCode()
简单数据的POJO被塞满我的代码和维护繁琐。
What are the libraries handling this automatically?
I prefer bytecode instrumentation over AOP approach due to performance reasons.
自动处理这个的库是什么?
由于性能原因,我更喜欢字节码检测而不是 AOP 方法。
Update:Topic of necessity of implementing equals() and hashCode() has been discussed, here's my point:
更新:已经讨论了实现 equals() 和 hashCode() 的必要性,这是我的观点:
Isn't it better to have it done right upfront with minimal effort rather than digging in the code, adding hC/eq when it comes to it?
与其深入研究代码并在涉及到它时添加 hC/eq,不如以最少的努力提前完成它不是更好吗?
采纳答案by joschi
Project Lombokprovides the annotation @EqualsAndHashCodewhich will generate equals()
and hashCode()
for your Java classes. Of course there are some drawbacks in comparison to manually implementing these methods, so make sure you read the "small print" on the linked page.
龙目岛项目提供了注释@EqualsAndHashCode,它将为您的Java 类生成equals()
和hashCode()
。当然,与手动实施这些方法相比,存在一些缺点,因此请务必阅读链接页面上的“小字”。
回答by KARASZI István
What about Guava's Objects.hashCode
and Objects.equal
?
回答by Andrejs
You can use Google's AutoValuelibrary to automatically generate immutable value classes with equals
and hashCode
. These value classes are somewhat similar to Scala's case classes or those generated by Lombok.
您可以使用 Google 的AutoValue库自动生成带有equals
和 的不可变值类hashCode
。这些值类有点类似于 Scala 的 case 类或 Lombok 生成的那些。
There's also a poston how to use it in an IDE.
还有一篇关于如何在 IDE 中使用它的帖子。
回答by Basil Bourque
Java 7 & later
Java 7 及更高版本
While not the panacea you requested, writing the hashCode
override is a bit easier now as of Java 7 and later.
虽然不是您要求的灵丹妙药,但从hashCode
Java 7 及更高版本开始,现在编写覆盖更容易一些。
Objects.hashCode
& Objects.hash
Objects.hashCode
& Objects.hash
As of Java 7, the Objects
class offers a couple of utility methods for generating hash code values.
从 Java 7 开始,Objects
该类提供了几个用于生成哈希码值的实用方法。
See my Answeron a related Question for more discussion.
- If your
hashCode
(andequals
) override is based on a single member of your class, useObjects.hashCode( member )
. - If your
hashCode
(andequals
) override is based on multiple attribute of your class, useObjects.hash( memberA , memberB , memberC )
.
- 如果您的
hashCode
(和equals
)覆盖基于您的类的单个成员,请使用Objects.hashCode( member )
. - 如果您的
hashCode
(和equals
)覆盖基于您的类的多个属性,请使用Objects.hash( memberA , memberB , memberC )
.
Single member, not tolerating a NULL
单个成员,不能容忍 NULL
@Override
public int hashCode() {
return this.member.hashCode() ; // Throws NullPointerException if member variable is null.
}
Single member, tolerating a NULL
单个成员,容忍 NULL
@Override
public int hashCode() {
return Objects.hashCode( this.member ) ; // Returns zero (0) if `this.member` is NULL, rather than throwing exception.
}
Multi-member
多人
@Override
public int hashCode() {
return Objects.hash( this.memberA , this.memberB , this.memberC ) ; // Hashes the result of all the passed objects' individual hash codes.
}
回答by rfeak
The Apache commons-lang library has a HashCodeBuilder and EqualsBuilder that will do some of the work for you and shorten those methods. There are even reflection versions that will do it all for you based on the fields in the POJOs. However, I wouldn't recommend that. Reflection can be slow (though not as bad as many think), and you should implement them to be sure that only the correct fields are considered for equality.
Apache commons-lang 库有一个 HashCodeBuilder 和 EqualsBuilder,它们将为您完成一些工作并缩短这些方法。甚至还有反射版本可以根据 POJO 中的字段为您完成所有工作。但是,我不建议这样做。反射可能很慢(虽然没有很多人想象的那么糟糕),你应该实现它们以确保只有正确的字段才被考虑为相等。
My question is, do you really need to do this? Often hashcode and equals on POJOs only need to be implemented for use with Maps or Sets. In the case of Maps, usually you would use an ID for a key, which isn't the Pojo itself. So, .... are you making work for yourself?
我的问题是,你真的需要这样做吗?通常 POJO 上的 hashcode 和 equals 只需要实现以与 Maps 或 Sets 一起使用。在 Maps 的情况下,通常您会使用 ID 作为键,它不是 Pojo 本身。那么,.... 你在为自己工作吗?