11

Consider i have one POJO having String class members :

class POJO {
  String name, address, emailId;
  equals() {

  }
  hashCode() {
    // How?    
  }
}

How can i combine hashCodes of my strings to form a hashCode for POJO?

VishalDevgire
  • 4,232
  • 10
  • 33
  • 59

4 Answers4

35

Java 7 has a utility method to create a hashcode which is good for most uses:

return Objects.hash(name, address, emailId);

You still need to make sure that your equals method is consistent. The two methods could look like:

@Override
public int hashCode() {
    return Objects.hash(name, address, emailId);
}

@Override
public boolean equals(Object obj) {
    if (obj == null) return false;
    if (getClass() != obj.getClass()) return false;
    final POJO other = (POJO) obj;
    if (!Objects.equals(this.name, other.name)) return false;
    if (!Objects.equals(this.address, other.address)) return false;
    if (!Objects.equals(this.emailId, other.emailId)) return false;
    return true;
}
assylias
  • 321,522
  • 82
  • 660
  • 783
9

You can use org.apache.commons.lang.builder.HashCodeBuilder class without worrying about hashcode implementation.It follows the contract between hashcode and equals.
For your class you have to do implementation like the following.

@Override    
public int hashCode(){
    HashCodeBuilder builder = new HashCodeBuilder();
    builder.append(name);
    builder.append(address);
    builder.append(emailId);
    return builder.toHashCode();
}
Prabhaker A
  • 8,317
  • 1
  • 18
  • 24
2

This is a pretty decent hashcode below.

   @Override
    public int hashCode() {
        int hash = 7;
        hash = 29 * hash + Objects.hashCode(this.name);
        hash = 29 * hash + Objects.hashCode(this.address);
        hash = 29 * hash + Objects.hashCode(this.emailId);
        return hash;
    }
Jatin
  • 31,116
  • 15
  • 98
  • 163
1

Do it like this:

public int hashCode()
{
            final int prime = 31;
            int result = 1;
            result = prime * result
                    + ((address == null) ? 0 : address.hashCode());
            result = prime * result
                    + ((emailId == null) ? 0 : emailId.hashCode());
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            return result;
}
Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
  • 1
    What are you adding on `prime * result` repeatedly? I think this doesn't add anything to the `hashCode`. – Boris the Spider Aug 05 '13 at 07:22
  • @BoristheSpider: Just to make hashcode unique. – Ankur Lathi Aug 05 '13 at 07:24
  • @BoristheSpider: http://stackoverflow.com/questions/3613102/why-use-a-prime-number-in-hashcode – Ankur Lathi Aug 05 '13 at 07:25
  • 2
    Yes, you are supposed to use a prime number, but to multiply each not addition to the `hashCode`, not scale the `hashCode` repeatedly by prime numbers. So `result += 7 * address`. Also use different primes for each field that way if two fields have values swapped the hashCode is more likely to be unique. – Boris the Spider Aug 05 '13 at 07:27
  • Generally people use hashcode with equals. – MayurB Aug 05 '13 at 07:29