简体   繁体   English

什么是HashCodeBuilder和EqualsBuilder,用于覆盖hashcode()和equals()方法?

[英]What is HashCodeBuilder and EqualsBuilder which is used in overriding the hashcode() and equals() method?

I have to override the equals() method and hascode() method for entity class. 我必须覆盖实体类的equals()方法和hascode()方法。 But My question is why to use the HashcodeBuilder and EqualsBuilder to implement it. 但我的问题是为什么要使用HashcodeBuilder和EqualsBuilder来实现它。

Which one is better among this two and why ? 这两者中哪一个更好,为什么?

  @Override
public int hashCode()
{
    return HashCodeBuilder.reflectionHashCode(this, false);
}

@Override
public boolean equals(Object obj)
{
    return EqualsBuilder.reflectionEquals(this, obj);
}

OR 要么

@Override
public int hashCode()
{
    final int prime = 31;
    int result = 1;
    result = prime * result + ((userKey == null) ? 0 : userKey.hashCode());
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    result = prime * result + ((userEntity == null) ? 0 : userEntity.hashCode());
    return result;
}

@Override
public boolean equals(Object obj)
{
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    UserKeyEntity other = (UserKeyEntity) obj;
    if (UserKey == null)
    {
        if (other.userKey != null)
            return false;
    }
    else if (!userKey.equals(other.userKey))
        return false;
    if (id == null)
    {
        if (other.id != null)
            return false;
    }
    else if (!id.equals(other.id))
        return false;
    if (userEntity == null)
    {
        if (other.userEntity != null)
            return false;
    }
    else if (!userEntity.equals(other.userEntity))
        return false;
    return true;
}

and why? 为什么?

the Second is by default created by the STS IDE. 默认情况下,第二个是由STS IDE创建的。 Please tell me what exactly the 1st Option is about and why to prefer? 请告诉我第一个选项到底是什么以及为什么要选择?

Personally, I wouldn't use reflection to compute equals and hashcode. 就个人而言,我不会使用反射来计算equals和hashcode。

As the doc states (for EqualsBuilder.reflectionEquals ): 正如doc所述(适用于EqualsBuilder.reflectionEquals ):

It uses AccessibleObject.setAccessible to gain access to private fields. 它使用AccessibleObject.setAccessible来访问私有字段。 This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. 这意味着如果未正确设置权限,则在安全管理器下运行时将抛出安全性异常。 It is also not as efficient as testing explicitly. 它也没有明确测试那么高效。 Non-primitive fields are compared using equals(). 使用equals()比较非原始字段。

So 所以

  • You are doing dangerous operations (and you are not even sure that you wouldn't get a SecurityException ) 你正在做危险的操作(你甚至不确定你不会得到SecurityException
  • It's less effective because you use reflection to compute those values 它的效果较差,因为您使用反射来计算这些值

As a personal point of view, I really feel like using reflection to compute equals and hashcode from your class is a non-sense. 从个人的角度来看,我真的觉得使用反射来计算你的类中的equals和hashcode是没有意义的。 It's like using the wrong tool. 这就像使用错误的工具。

Since you are already using a third party library, I would use the HashCodeBuilder like this : 由于您已经在使用第三方库,我会像这样使用HashCodeBuilder

@Override
public int hashCode() {
    return new HashCodeBuilder().append(userKey)
                                .append(id)
                                .append(userEntity)
                                .toHashCode();
}

and same with equals: 与equals相同:

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    UserKeyEntity other = (UserKeyEntity) obj;
    return new EqualsBuilder().append(userKey, other.userKey)
                              .append(id, other.id)
                              .append(userEntity, other.userEntity)
                              .isEquals();
}

which is a bit more readable than the ones generated by Eclipse, and don't use reflection. 这比Eclipse生成的更具可读性,并且不使用反射。

HashcodeBuilder and EqualsBuilder will affect the performance because it uses reflection,it will be slower than than the second one. HashcodeBuilder和EqualsBuilder会影响性能,因为它使用反射,它会慢于第二个。

You can use the one generated by IDE over HashcodeBuilder and EqualsBuilder. 您可以使用IDE通过HashcodeBuilder和EqualsBuilder生成的那个。

HashcodeBuilder and EqualsBuilder will be easy to read, understand and its dynamic. HashcodeBuilder和EqualsBuilder将易于阅读,理解和动态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM