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?
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?
 
    
    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;
}
 
    
    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();
}
 
    
    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;
    }
 
    
    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;
}
