We have several data types defined for our service response and request for objects in the model. We recently discovered the need to implement ToString, HashCode, and Equals in all of these types in order to use them for comparison and approval. Confirmation from several sources, such as What problems should be considered when redefining equals and hashCode in Java? The correct way to implement equals contract , etc. we followed implementations of toString, equals and hashcode using org.apache.commons.lang3.builder.EqualsBuilder , HashCodeBuilder and ToStringBuilder as follows:
Response.java
import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.commons.lang3.builder.ToStringBuilder; public class Response { private Integer value; private Currency currency; private Object edited; public Response() { } public Response(Integer value, Currency currency, Object edited) { this.value = value; this.currency = currency; this.edited = edited; } public Currency getCurrency() { return currency; } public void setCurrency(Currency currency) { this.currency = currency; } public Integer getValue() { return value; } public void setValue(Integer value) { this.value = value; } public Object getEdited() { return edited; } public void setEdited(Object edited) { this.edited = edited; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Response Response = (Response) o; return new EqualsBuilder().append(value, Response.value).append(currency, Response.currency) .append(edited, Response.edited).isEquals(); } @Override public int hashCode() { return new HashCodeBuilder(17, 37).append(value).append(currency).append(edited).toHashCode(); } @Override public String toString() { return "Response{" + "value=" + value + ", currency=" + currency + ", edited=" + edited + '}'; } }
Currency.java
public enum Currency { INR }
When implementing these programs using the default version of the library, there is a thought about enumerations that comes to our mind -
Is it correct to use the default hash code and is equal to the library when the data type can contain parameters, including enumerations? Is there a library (within the limits of public rights) that supports the implementation of the correct optimized solution for overriding the hashcode implementation and is equal to?
On the note side, is the library implementation in need of improvement here, or is it correct for what exists?
Edit : added the implementation in the Object ( edited ) field to the class. A concern arises if I redefine hashCode and equal implementation for them.
Ultimately, I use a hashcode of an object that is different for different instances, since this is basically a memory mapping address?
Change 2 . I also see concerns about inconsistent implementation of HashCode values ββfor Enum on JIRA
java equals hashcode enums apache-commons-lang3
nullpointer
source share