开发者

Common method for comparing any two Java objects

I want to compare two Java objects without overriding equals method. Since I need to override equals method in n number of Classes I have, I am in need of a common utility method where we can compare two Java objects.

Something like:

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 out to write a common utility for comparing any Java objects

Hope by using Field class, and getClass me开发者_高级运维thod we can achieve it. Please guide me.


Have a look at EqualsBuilder.reflectionEquals in the Apache Commons library.

From the documentation:

This method uses reflection to determine if the two Objects are equal.

It uses AccessibleObject.setAccessible to gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly.


So, in your example it would look like:

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);


This may be what you are looking for EqualsBuilder.reflectionEquals

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


Try Apache Commons EqualsBuilder#reflectionEquals method and check if it meets your needs.


First, is there a compelling reason why you don't want to use the equals method? The equals method is used internally by collection classes and such so you will have to obey the equals contract.

If there is one, then yes, you can do a field by field comparison using reflection. Get the list of fields of the class and for each field check the value. You will also have to ensure that they are both instance of the same class upfront.


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 that other people have suggested, most notably the Apache Commons project.


You might want to consider using Shazamcrest.

From their 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 would write:

assertThat(actualPerson, sameBeanAs(expectedPerson));


Try this. This works perfectly but needs more heap space if the objects that you compares are huge. org.unitils.reflectionassert.ReflectionAssert.assertReflectionEquals(obj1, obj2)

http://www.unitils.org/

You can get the jar from below.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜