Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore 1 field in Java equals() for custom object?

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 ?

like image 323
newLearner Avatar asked Dec 22 '21 13:12

newLearner


People also ask

How do you ignore one field in a JSON response?

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.

Can we use .equals for objects in Java?

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 .

How do you use equals method in Java for objects?

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.

Is it possible for equals () to return false even if contents of two objects are same?

Yes. You can also override the equals() method and play with it.

What is the use of equals in Java?

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.

How do you compare two fields in Java?

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.

How to override this equals method in Java?

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.

Can We pass a null object as the argument of equals ()?

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:


1 Answers

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

like image 72
Nico Van Belle Avatar answered Oct 23 '22 10:10

Nico Van Belle