Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compact equals and hashcode

Tags:

java

eclipse

I have a bean with 4 attributes:

user
institutionId
groupId
postingDate

I use Eclipse to generate equals and hashcode but the resulting code is not pretty. Is there a compact way to do the same? Assuming I want equals & hashcode to use all the attributes or a subset of them.

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((groupId == null) ? 0 : groupId.hashCode());
    result = prime * result + ((institutionId == null) ? 0 : institutionId.hashCode());
    result = prime * result + ((postingDate == null) ? 0 : postingDate.hashCode());
    result = prime * result + ((user == null) ? 0 : user.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;
    ManGroupKey other = (ManGroupKey) obj;
    if (groupId == null) {
        if (other.groupId != null)
            return false;
    } else if (!groupId.equals(other.groupId))
        return false;
    if (institutionId == null) {
        if (other.institutionId != null)
            return false;
    } else if (!institutionId.equals(other.institutionId))
        return false;
    if (postingDate == null) {
        if (other.postingDate != null)
            return false;
    } else if (!postingDate.equals(other.postingDate))
        return false;
    if (user == null) {
        if (other.user != null)
            return false;
    } else if (!user.equals(other.user))
        return false;
    return true;
}
like image 463
Lluis Martinez Avatar asked Mar 14 '14 16:03

Lluis Martinez


People also ask

What is the hashCode () and equals () function?

The hashcode() method returns the same hash value when called on two objects, which are equal according to the equals() method. And if the objects are unequal, it usually returns different hash values.

What is the difference between equals and hashCode?

The value returned by hashCode() is the object's hash code, which is the object's memory address in hexadecimal. equals() checks if the two object references are same. If two objects are equal then their hashCode must be the same, but the reverse is not true.

Why we need to override hashCode and equals method?

If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. It is not required that if two objects are unequal according to the equals(java.

Why hashCode should be overridden when Equals is overridden?

You must override hashCode() in every class that overrides equals(). Failure to do so will result in a violation of the general contract for Object. hashCode(), which will prevent your class from functioning properly in conjunction with all hash-based collections, including HashMap, HashSet, and Hashtable.


Video Answer


2 Answers

In Java 7


public int hashCode() {
    return Objects.hash(groupId, institutionId, postingDate, user);
}


public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;

    // cast to correct class
    Target o = (Target)obj;

    return Objects.equals(groupId, o.groupId) &&
           Objects.equals(institutionId, o.institutionId) &&
           Objects.equals(postingDate, o.postingDate) &&
           Objects.equals(user, o.user);
}
like image 93
Leonard Brünings Avatar answered Oct 04 '22 16:10

Leonard Brünings


You could compact the code down, but the odds are far higher that you would introduce bugs than that you would do anything useful. All the parts of the equals and hash code method are there for a reason.

If it's bothering you most IDEs have a folding editor, just click the little yellow box (usually) and all the contents of the method get hidden away.

like image 30
Tim B Avatar answered Oct 04 '22 15:10

Tim B