how to compare two objects and find the fields/properties changed?

javers compare collections
how to compare two pojo objects in java
java compare two objects field by field java 8
java object comparison utility
beanutils compare two objects
how to compare two objects in java
guava compare two objects
compare two custom objects in java

I need to write some generic solution to find out what properties in two objects have changed and return the changed properties (not the value).

class Student {
    public String name;
    public Address address;
    public int age;
}

class Address {
    public String hno;
    public String street;
    public int pin;
}

public class Test {
    public static void main(String... arg) {

        Student s1 = new Student();
        s1.name = "Krishna";
        s1.age = 30;
        s1.address = new Address();
        s1.address.hno = "2-2-22";
        s1.address.street = "somewhere";
        s1.address.pin = 123;

        Student s2 = new Student();
        s2.name = "Krishna";
        s2.age = 20;
        s2.address = new Address();
        s2.address.hno = "1-1-11";
        s2.address.street = "nowhere";
        s2.address.pin = 123;

        List<String> = difference(s1, s2);
        // expected result
        // Student.age
        // Student.address.hno
        // Student.address.street
    }
}

Can anyone please suggest some solution?

PS

Overriding equals/hashcode is not an option for me.

I have written the below code but I am not sure how to identify if a type is complex (for example Address)

private static List<String> difference(Student s1, Student s2) throws IllegalArgumentException, IllegalAccessException {

        for(Field field: Student.class.getDeclaredFields()) {
            System.out.println(field.getName() + " " +field.get(s1).equals(field.get(s2)));
        }
        return null;
    }

I think this is the method you searching for:

 private static List<String> difference(Student s1, Student s2) throws IllegalAccessException {
     List<String> changedProperties = new ArrayList<>();
     for (Field field : s1.getClass().getDeclaredFields()) {
        // You might want to set modifier to public first (if it is not public yet)
        field.setAccessible(true);
        Object value1 = field.get(s1);
        Object value2 = field.get(s2); 
        if (value1 != null && value2 != null) {
            System.out.println(field.getName() + "=" + value1);
            System.out.println(field.getName() + "=" + value2);
            if (!Objects.equals(value1, value2)) {
                changedProperties.add(field.getName());
            }
        }
    }
    return changedProperties;
 }

Use reflection to compare the properties of two objects foreach (FieldInfo field in fields) Since the O/R mapper and FileHelpers used the same class, using this comparison method to determine if the objects were “equal” implementing an audit trail that requires us to to record what data was changed. You can use reflection to identify the properties to compare. Then it's just a matter of using a loop instead of writing a new if clause for each property. If you only want to compare some properties, you can define a custom Attribute, either to opt-in properties for the comparison, or to opt-out. – Peter Duniho Oct 17 '14 at 19:29

Although it's an old question, I would like to add an idea:-

  1. Use JAXB to convert java objects to XML.
  2. Use XMLUnit to compare differences in XMLs.

It will give you the names of fields that have changed, the before and after values of those fields, etc.

I admit this is not the best way because it has overhead of marshalling and running XML comparison; and the need to add a couple of libraries. But it might be better than writing field by field comparison (specially deep comparison) and reflection for complex model objects.

We can find the latest version over on Maven Central. of the codebase, the name of the person with the same id field was changed. We can compare those two objects using the compare() method from the JaVers class: ? Briefly, Objects Comparer is an object-to-object comparer that allows us to compare objects recursively member by member and to define custom comparison rules for certain properties, fields or types. Objects Comparer can be considered as a ready-to-use framework or as a starting point for similar solutions.

Comparing the properties of two objects via Reflection and C# question, but I have heavily modified and expanded the original concept. I hope you find these helper methods useful, this article will be updated if and when  Able to compare two objects of any type, provided they are the same type as each other. Able to exclude certain properties. Able to determine the primary key field and record that value. Let's set up an environment where we can achieve these goals.

How to find the differences between two objects in Jav. Class only has a few properties, you can write the code by comparing each property. The PropertyInfo class has a GetValue method that can be used to read the value of a property. Its most basic usage is to pass in the instance object (or null if you want to read a static property) and any index parameters (or null if no index parameters are supported). Hide Copy Code.

Two objects or values are considered equivalent if at least one of the same type and all of their properties are equal by comparing them with angular.equals . Here's an example of two items that implement our own IWeapon<T> interface, which in turn extends the IEquatable<T> interface. Implement the one, and you get the other for free. public class Sword : IWeapon< Sword > { public enum SwordLevel { Fighter, Master, Tempered, Golden } public SwordLevel Strength { get; set; } public bool Equals(Sword other) { return Strength == other ?.

java-object-diff is a simple, yet powerful library to find differences between Java objects. It takes two objects and generates a tree structure that represents any to use reflection to scan the object for fields or getters and use them to compare the with nested objects that also need to be compared on a per-property basis? active oldest votes. 1. Although it's an old question, I would like to add an idea:-. Use JAXB to convert java objects to XML. Use XMLUnit to compare differences in XMLs. It will give you the names of fields that have changed, the before and after values of those fields, etc.

Comments
  • Would there be anything wrong with an if else statement?
  • You can use reflection. It will be the most generic one. Just iterate over the fields of both the object and compare values. If values differ, then field.getName should be able to give you the name of a field.
  • difference between s1 & s2 or difference between new s1 and previous s1
  • this sounds like an XY problem. why do you think you need this?
  • @Eugene I need this to persist the modified fields in a table to track the changes.
  • Hey here no need to return the values. Just two objects are equals or not as per the properties.
  • Even though I override equals method, I wont be able to know what fields have changed in Address. How do I solve this issue?
  • age,hno,street are the properties he has to compare the objects.