Difference between two instances of the same class

I have two instances of the same class. I need to find property (s) that is different from them (basically the value of the property, for example firstName may be different in both). Fields are primitive, complex, and also collections.

Basically, I need to find the differences between the two instances, and if the fields are different in both instances, I will copy the field value from the first instance to the third instance (diff object).

I think I can use reflection, but the classes are very complex, and this can cause errors.

+6
java javabeans
source share
4 answers

Your question is a bit unclear. First you said “two instances of the same class”, then you said “classes are very complex”. Is this a general solution for finding differences between instances for any class, or is it just a specific case? Another ambiguity is what you mean by "copy of differences." For example, if it is a row, then what is the difference between the two rows that will be copied to a new instance? If it's a collection, what's the difference between collections? What if you have two ArrayLists that have the same things in different orders.

If this is a specific case, you can simply create a method in a class in which you pass an instance of the same class. You can then iterate over each field and compare the differences.

public TheClass difference(TheClass that) { TheClass newClass = new TheClas() if (this.getName().equals(that.getName()) == false ) { newClass.setName(that.getName()); } ... } 

But this can get out of control depending on how deep your graph is.

Perhaps the linker template might come in handy here if you are trying to create a one-stop / reusable solution. You can look at the Apache Commons code base and see how they implement HashCodeBuilder and ToStringBuilder. They even have a mirrored version of the utility. See how they deal with deep and shallow peers.

+4
source share

There is no other way than using reflection if you want to do this in general. If your fields have getters, you can continue the comparison with the equals method. Then you know the fields at compile time.

+2
source share

write a type method

 public YourClass diff(YourClass other) { // diff code here } 

or

 public static YourClass diff(YourClass first, YourClass second) { // diff code here } 

and wrap some unit tests around implementations.

Reflection may seem more complex, but it has the following advantages:

1) It can be shared, and you can make the same code with any class
2) It does not need to be changed if the class evolves.

If you want to go with reflection, you can use Class.getDeclaredFields () (google for "java class api 6") to get all the fields in the class, and then you can use the api field to get the values ​​for the object. If you want to be really fantastic, you have a static String [] "diffIgnoredFields" and specify the names of the fields that you want to ignore in diff.

+1
source share

You need to be careful. Some formulations of this problem are basically the same as the Subgraph Isomorphism Problem , which, as you know, NP is completed .

0
source share

All Articles