日志数据前后计算

日志数据前后计算

public class CompareFieldUtil {

    public static void fieldsEquals(List<Object> list) throws NoSuchFieldException {
        Object source = list.get(0);
        Object target = list.get(1);
        if (source == null || target == null) {
            return;
        }

        Class<?> sourceClass = source.getClass();
        Class<?> targetClass = target.getClass();

        Field[] sourceFields = source.getClass().getDeclaredFields();
        Field[] targetFields = target.getClass().getDeclaredFields();

        for (Field sourceField : sourceFields) {
            sourceField.setAccessible(true);
            String fieldName = sourceField.getName();
            Field targetField = targetClass.getDeclaredField(fieldName);
            targetField.setAccessible(true);
            Object sourceValue = null;
            Object targetValue = null;
            try {
                sourceValue = sourceField.get(source);
                targetValue = targetField.get(target);

                // 对于集合属性,需要比较其中元素
                if (sourceValue instanceof List && targetValue instanceof List) {
                    List<?> coll1 = (List<?>) sourceValue;
                    List<?> coll2 = (List<?>) targetValue;
                    for (int i = 0; i < coll1.size(); i++) {
                        fieldsEquals(Arrays.asList(coll1.get(i), coll2.get(i)));
                    }
                } else {
                    if (sourceValue == null && targetValue == null){
                        return;
                    }
                    if ((sourceValue != null && sourceValue.equals(targetValue)) || (targetValue != null && targetValue.equals(sourceValue))) {
                        targetField.set(target, null);
                        sourceField.set(source, null);
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    // 测试方法
    public static void main(String[] args) throws NoSuchFieldException {
        // 创建两个对象实例,并设置相同的属性值
        // 示例中只比较了部分属性,实际使用时需要比较所有需要的属性
        MyClass obj1 = new MyClass();
        obj1.setId(1);
        obj1.setName("Test");
        obj1.setItems(Arrays.asList(new Item(1, "Item1"), new Item(2, "Item2")));

        MyClass obj2 = new MyClass();
        obj2.setId(2);
        obj2.setName("Test");
        obj2.setItems(Arrays.asList(new Item(1, "Item2"), new Item(2, "Item2")));

        fieldsEquals(Arrays.asList(obj1, obj2));
        System.out.println("obj1: " + JSON.toJSONString(obj1) + "obj2: " + JSON.toJSONString(obj2));
    }

    @Data
    static class MyClass {
        private Integer id;
        private String name;
        private List<Item> items;
    }

    @Data
    @AllArgsConstructor
    static class Item {
        private Integer id;
        private String name;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值