General method for comparing any two Java objects

I want to compare two Java objects without overriding the equals method . Since I need to redefine the equals method in n number of classes that I have, I need a general utility method where we can compare two Java objects.

Sort of:

A a1,a2; B b1,b2; C c1,c2; ----- ----- boolean isEqual1 = new ObjectComparator().isEquals(a1 , a2); boolean isEqual2 = new ObjectComparator().isEquals(b1 , b2); boolean isEqual3 = new ObjectComparator().isEquals(c1 , c2); 

Please help me write a general utility to compare any Java objects.

Hopefully using the Field class and getClass , we can achieve it. Please guide me.

+6
source share
7 answers

See EqualsBuilder.reflectionEquals in the Apache Commons library.

From the documentation :

This method uses reflection to determine if two objects are equal .

It uses AccessibleObject.setAccessible to access private fields. This means that it throws a security exception if it runs under the security manager, if the permissions are not configured correctly. It is also not as effective as testing explicitly.


So, in your example, it will look like this:

 A a1,a2; B b1,b2; C c1,c2; ----- ----- boolean isEqual1 = EqualsBuilder.reflectionEquals(a1 , a2); boolean isEqual2 = EqualsBuilder.reflectionEquals(b1 , b2); boolean isEqual3 = EqualsBuilder.reflectionEquals(c1 , c2); 
+10
source

This may be what you are looking for EqualsBuilder.reflectionEquals

 public boolean equals(Object obj) { return EqualsBuilder.reflectionEquals(this, obj); } 
+3
source

Try the Apache Commons EqualsBuilder # reflectionEquals method and see if it fits your needs.

+2
source

Firstly, is there a good reason why you do not want to use the equals method? The equals method is used inside the collection class, and so you have to obey an equal contract.

If there is one, then yes, you can make a field by comparing fields using reflection. Get a list of class fields and check the value for each field. You will also need to make sure that they are both instances of the same class.

+1
source

You want Objects.equals(a,b) , available in Java 1.7, in the java.util package

If you are using a version earlier than Java 1.7, you can use one of the libraries suggested by other people, primarily the Apache Commons project.

0
source

You might want to use Shazamcrest .

On github page:

Having a Person bean with the following structure:

 Person person |-- String name |-- String surname |-- Address address |-- String streetName |-- int streetNumber |-- String postcode 

to compare two Person beans with Shazamcrest we will write:

 assertThat(actualPerson, sameBeanAs(expectedPerson)); 
0
source

Try it. This works great, but requires more heap space if the objects you are comparing are huge. org.unitils.reflectionassert.ReflectionAssert.assertReflectionEquals (obj1, obj2)

http://www.unitils.org/

You can get a can from below.

 <!-- https://mvnrepository.com/artifact/org.unitils/unitils --> <dependency> <groupId>org.unitils</groupId> <artifactId>unitils</artifactId> <version>2.2</version> </dependency> 
0
source

Source: https://habr.com/ru/post/896785/


All Articles