Consider i have one POJO having String class members :
class POJO {
String name, address, emailId;
equals() {
}
hashCode() {
// How?
}
}
How can i combine hashCode
s 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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With