3

Can you suggest any way to compare two object states?

Is there any API to achieve this? Or, only Reflection is the answer?

Please note: I do not need mere equals() / compare() methods, as they are only for sorting, I need entire object graph comparison which will give me a list of matched or unmatched values as a result.

javaCurious
  • 140
  • 2
  • 14

5 Answers5

2

override hashCode & equal method in your class that will help you for comparing java object.

please refer below links

link1

link2

link3

Community
  • 1
  • 1
Pushkar
  • 727
  • 1
  • 7
  • 21
2

Apache Commons Lang introduces a Diffable<T> interface, which an object can implement if it wants to be deeply comparable to other objects. Similar to Comparable<T>, this interface requires that an object can deeply compare itself against another object of the same type and produce a list of the differences.

A DiffBuilder class exists to help you implement the Diffable.diff() method.

Currently there's not a reflection-based variant of this, something that's actually been forgotten about since I accidentally closed LANG-637 without implementing one. Ooops.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • How much useful is this API, say i need to compare two objects of same type, deep nested comparison, it will have collections, also within a collection order of insertion might be different, so can it recursively compare objects within a list also. – Ankur Singhal Nov 17 '14 at 13:20
  • Good API. But not generic enough. I do not have access to the source code of my POJOs (i.e. objects-to-compare). – javaCurious Nov 17 '14 at 14:17
1

Use the CompareToBuilder class from apache's commons-lang. It uses reflection and provides some flexibility to determine what to compare and what not. These are some hopefully self-explanatory methods:

CompareToBuilder.reflectionCompare(Object lhs, Object rhs, boolean compareTransients)
CompareToBuilder.reflectionCompare(Object lhs, Object rhs, String[] excludeFields) {
CompareToBuilder.reflectionCompare(Object lhs, Object rhs, boolean compareTransients, Class reflectUpToClass) 

It will go through the entire object graph using reflection to determine whether the objects are equality.

Jeronimo Backes
  • 6,141
  • 2
  • 25
  • 29
1

Well, you can use reflection to get the field values of a class. And if the fields are the same and they all have associated getters, you can compare two objects if you know their type.

public void genericCompare(Object one, Object two) {
    Fields [] oneFields = one.getClass().getFields();
    Fields [] twoFields = two.getClass().getFields();
    ...
    /** assuming they have the same number of fields */
    for (int t = 0; t < oneFields.length; t++) {
        System.out.println("Object one \'" + oneFields[t].getName() + "\' has value " + oneFields[t].get(one));
        System.out.println("Object two \'" + twoFields[t].getName() + "\' has value " + twoFields[t].get(two));
    } 
} 
Ian2thedv
  • 2,691
  • 2
  • 26
  • 47
0

XMLUnit will work, it compares the list references recursively, also it has option to exclude fields which you do not wish to compare.

 String expectedXML = "some xml";
 String actualXML = "some xml";

 DetailedDiff diff1 = new DetailedDiff(XMLUnit.compareXML(expectedXML, actualXML));
 diff1.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
 System.out.println("Differences found: " + diff1.getAllDifferences().toString());

RecursiveElementNameAndTextQualifier

Compares all Element and Text nodes in two pieces of XML. Allows elements of complex, deeply nested types that are returned in different orders but have the same content to be recognized as comparable.

Refer this as well.

Also very soon i am going to blog this here.

Generating XML

import com.thoughtworks.xstream.XStream;

 public static String writeToXml(Object objectToWrite) {
        XStream  xstream   = new XStream();
        return xstream.toXML(objectToWrite);
    }

Dependency

<dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
</dependency>
Community
  • 1
  • 1
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116