Let's say I have a class Customer:
public class Customer {
private String firstName;
private String lastName;
private String doNotAddMeToEquals;
//Getters and Setters below
}
I'm using the Guava Eclipse Plugin in Eclipse to generate my equals() and hashCode() methods; however, I could just as well use the eclipse -> Source -> Generate HashCode / Equals. Either way...doesn't matter.
Is there a way to Annotate property doNotAddMeToEquals such that when I generate the equals & hashcode methods with the guava plugin that property doesn't show in the list?
Without altering the plugin or creating a template.
Thanks in Advance!!
You must override hashCode() in every class that overrides equals(). Failure to do so will result in a violation of the general contract for Object. hashCode(), which will prevent your class from functioning properly in conjunction with all hash-based collections, including HashMap, HashSet, and Hashtable.
if a class overrides equals, it must override hashCode. when they are both overridden, equals and hashCode must use the same set of fields. if two objects are equal, then their hashCode values must be equal as well. if the object is immutable, then hashCode is a candidate for caching and lazy initialization.
The annotation @Data with inheritance produces the next warning: Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.
If you need to write your own equals methods, you should always override canEqual if you change equals and hashCode . NEW in Lombok 1.14. 0: To put annotations on the other parameter of the equals (and, if relevant, canEqual ) method, you can use onParam=@__({@AnnotationsHere}) . Be careful though!
It sounds like what you want is something like this:
http://projectlombok.org/features/EqualsAndHashCode.html
It lets you use annotations to drive what properties are included in the equals and hashcode methods.
Using Lombok you can exclude properties from hashcode and equals like such as:
@EqualsAndHashCode(exclude = {"nameOfField"})
That would be in your case
@EqualsAndHashCode(exclude = {"doNotAddMeToEqualsAndHashCode"})
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