Java: Automatic equals() and hashCode()

末鹿安然 提交于 2019-12-21 03:36:29

问题


Implementing equals() and hashCode() for simple data POJOs is cluttering my code and maintaining is tedious.

What are the libraries handling this automatically?
I prefer bytecode instrumentation over AOP approach due to performance reasons.

Update: Topic of necessity of implementing equals() and hashCode() has been discussed, here's my point:

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?


回答1:


Project Lombok provides the annotation @EqualsAndHashCode which 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.




回答2:


What about Guava's Objects.hashCode and Objects.equal?




回答3:


You can use Google's AutoValue library 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.

There's also a post on how to use it in an IDE.




回答4:


Java 7 & later

While not the panacea you requested, writing the hashCode override is a bit easier now as of Java 7 and later.

Objects.hashCode & Objects.hash

As of Java 7, the Objects class offers a couple of utility methods for generating hash code values.

See my Answer on a related Question for more discussion.

  • If your hashCode (and equals) override is based on a single member of your class, use Objects.hashCode( member ).
  • If your hashCode (and equals) override is based on multiple attribute of your class, use Objects.hash( memberA , memberB , memberC ).

Single member, not tolerating a NULL

@Override
public int hashCode() {
    return this.member.hashCode() ;  // Throws NullPointerException if member variable is null.
}

Single member, tolerating a 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.  
}



回答5:


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.

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?



来源:https://stackoverflow.com/questions/6959307/java-automatic-equals-and-hashcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!