I have a Person
object which has 20 fields and stored in dynamoDB.
I want to create a new Person
object based on some input, check if the same object exists in the Database or not. If it exists, I want to compare the 2 objects on the basis of 19 fields out of 20
. The field to ignore is a boolean flag check.
I am using Lombok @Data
to generate the equals method.
Is there a way to do this without having to write a full fledged overriden equals
method myself ?
The Jackson @JsonIgnore annotation can be used to ignore a certain property or field of a Java object. The property can be ignored both when reading JSON into Java objects and when writing Java objects into JSON.
The equals() method is a static method of the Objects class that accepts two objects and checks if the objects are equal. If both the objects point to null , then equals() returns true .
If the two objects have the same values, equals() will return true . In the second comparison, equals() checks to see whether the passed object is null, or if it's typed as a different class. If it's a different class then the objects are not equal. Finally, equals() compares the objects' fields.
Yes. You can also override the equals() method and play with it.
Java equals method is a method of the Java Object class. This object class is the root of the class hierarchy in Java. i.e. every class in Java has class Object as its superclass. Hence, all objects and arrays implement the methods of this object class.
The equals()method of java.lang.reflect.Fieldis used to compare two field objects. This method compares two field objects and returns true if both objects are equal otherwise false. The two Field objects are considered equal if and only if when they were declared by the same class and have the same name and type.
This equals method does this by implementing an equivalence relation on objects which are non-null reference values. To override this method, it is usually necessary to override another method called hashCode. This is to fulfill the contract for the hashCode method as it says that objects which are equal must have equal hash codes.
We should note that we can pass a null object as the argument of the method, but of course, not as the object we call the method upon. We can use the equals () method with an object of our own. Let's say we have a Person class:
You can also use Lombok's @EqualsAndHashCode in conjunction with an exclude.
@EqualsAndHashCode
public class MyObject{
@EqualsAndHashCode.Exclude
Object fieldToExclude;
}
Instead of choosing which to exclude you can also opt to choose the ones you wish to include like so. All which is not annotated with Include will not be used in the equals & hashcode implementation.
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class MyObject{
@EqualsAndHashCode.Include
Object fieldToInclude;
}
Lombok EqualsAndHashCode documentation
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