简体   繁体   中英

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. But My question is why to use the HashcodeBuilder and EqualsBuilder to implement it.

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. 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.

As the doc states (for EqualsBuilder.reflectionEquals ):

It uses AccessibleObject.setAccessible to gain access to private fields. 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().

So

  • You are doing dangerous operations (and you are not even sure that you wouldn't get a 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. It's like using the wrong tool.

Since you are already using a third party library, I would use the HashCodeBuilder like this :

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

and same with 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.

HashcodeBuilder and EqualsBuilder will affect the performance because it uses reflection,it will be slower than than the second one.

You can use the one generated by IDE over HashcodeBuilder and EqualsBuilder.

HashcodeBuilder and EqualsBuilder will be easy to read, understand and its dynamic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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