Skip to content
Advertisement

Comparator.comparing(…).thenComparing(…) find out which fields did not match

I am trying to compare two objects of same class and the goal is to compare them as well as identify which fields didn’t match.

Example of my domain class

JavaScript

Two objects

JavaScript

Comparing both objects

JavaScript

result will be 0 because name & fullTime fields are not matching with each other.

But I also want to produce a list of fields which didn’t match.. like below

JavaScript

Can I do it in a nicer way, other than bunch of if() else

Advertisement

Answer

Check out DiffBuilder. It can report which items are different.

JavaScript

DiffResult has, among other things, a getDiffs() method that you can loop over to find what differs.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement