java 逐个字段比较两个对象并显示差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32196069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:48:07  来源:igfitidea点击:

Compare two objects field by field and show the difference

java

提问by Rohit K

I want to compare two object i.e two database rows field by field. e.g. Object1[name="ABC", age=29, email="[email protected]"]and Object2[name="XYZ", age=29, email="[email protected]"]

我想逐个字段比较两个对象,即两个数据库行。例如Object1[name="ABC", age=29, email="[email protected]"]Object2[name="XYZ", age=29, email="[email protected]"]

suppose I want to compare these two object and I want output like this

假设我想比较这两个对象并且我想要这样的输出

[{
 "fieldName" : "email",
 "OldObjectValue" : "[email protected]",
 "NewObjectValue" : "[email protected]"
},
{
 "fieldName" : "name",
 "OldObjectValue" : "ABC",
 "NewObjectValue" : "XYZ"
}]

Here age is same so age field is not present in output.

这里年龄相同,因此输出中不存在年龄字段。

If this is possible by doing generic method using reflection please provide some code. because I have not worked on reflection yet. Please help.

如果可以通过使用反射进行泛型方法来实现,请提供一些代码。因为我还没有进行反思。请帮忙。

回答by isurujay

According to your requirement you can do this as follow.

根据您的要求,您可以执行以下操作。

you can take two database rows to two objects. Eg: SampleObject

您可以将两个数据库行转换为两个对象。例如:样本对象

public class SampleObject {

private String name;
private int age;
private String email;   

public SampleObject(String name, int age, String email) {
    this.name = name;
    this.age = age;
    this.email = email;
}
.
.

I imagine your results will be an object too. Eg : ResultObject

我想你的结果也将是一个对象。例如:结果对象

public class ResultObject {

private String fieldName;
private String OldObjectValue;
private String NewObjectValue;
.
.

You can just define a compareField kind of method in SampleObject

你可以在 SampleObject 中定义一个 compareField 类型的方法

public List<ResultObject> compareFields(SampleObject object) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{
    List<ResultObject> resultList = new ArrayList<ResultObject>();      
    Field[] fields = this.getClass().getDeclaredFields();

    for(Field field : fields){
        if(!field.get(this).equals(field.get(object))){
            ResultObject resultObject = new ResultObject();
            resultObject.setFieldName(field.getName());
            resultObject.setOldObjectValue(field.get(this).toString());
            resultObject.setNewObjectValue(field.get(object).toString());
            resultList.add(resultObject);
        }
    }
    return resultList;
}

Then you can make it work.

然后你就可以让它工作了。

SampleObject object1 = new SampleObject("ABC", 29, "[email protected]");
    SampleObject object2 = new SampleObject("XYZ", 29, "[email protected]");

    List<ResultObject> resultList = object1.compareFields(object2);

Thanks

谢谢