Apache Commons Lang3 Hashcode, Equals and ToString, including Enums

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

+7
java equals hashcode enums apache-commons-lang3
source share

No one has answered this question yet.

See similar questions:

617
What issues should be considered when redefining peers and hashCode in Java?
43
What is the reason for Enum.hashCode ()?
17
The right way to implement an equal contract
0
Do I need to override equals / hashcode for enums that are placed in hashmap

or similar:

1555
Comparing Java enumeration elements: == or equals ()?
617
What issues should be considered when redefining peers and hashCode in Java?
349
Why do I need to override the equals and hashCode methods in Java?
146
Apache Commons equals / hashCode builder
eleven
How hash codes for enumerations are computed in Java and combine enum hash codes for a HashMap key
8
Unit testing is equal and hashcode is a complicated story
2
subtleties of working with equals and hashCode in the Java interface
one
Will a Linkedlist be formed if we override both hashCode () and equals ()?
0
Does hashCode () return value for enumeration values ​​with runtime
0
Does the compiler or JVM contain a hashCode / equals contract?

All Articles