首页 > 编程语言 >Java Bean 对象之间找不同的字段值

Java Bean 对象之间找不同的字段值

时间:2022-11-21 18:57:00浏览次数:42  
标签:段值 Java target Object Collection source Bean o2 o1

@SuppressWarnings("all")
public class BeanDifferentUtils {

    private BeanDifferentUtils() {
        //do nothing
    }

    /**
     * @param source
     * @param target
     * @param collectionComplement 如果是数组字段,那么先算并集然后去掉并集
     * @return
     * @throws IntrospectionException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static JSONObject diffField(Object source, Object target, boolean collectionComplement) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
        JSONObject diff = new JSONObject();
        Assert.notNull(source, "Source must not be null");
        Assert.notNull(target, "Target must not be null");
        if (source.getClass() != target.getClass()) {
            throw new IllegalArgumentException("Source object and target object are not of the same type");
        }
        Class<?> compareObj = source.getClass();
        Field[] fields = compareObj.getDeclaredFields();
        for (Field field : fields) {
            PropertyDescriptor pd = new PropertyDescriptor(field.getName(), compareObj);
            Method getMethod = pd.getReadMethod();
            Object o1 = getMethod.invoke(source);
            Object o2 = getMethod.invoke(target);
            if (o1 instanceof Collection && o2 instanceof Collection) {
                boolean equals = deepEquals(o1, o2);
                if (!equals) {
                    if (collectionComplement) {
                        Collection<Object> beMixed = (Collection<Object>) getBeMixed(o1, o2);
                        diff.put(field.getName(), Different.getInstance(((Collection) o1).removeAll(beMixed), ((Collection) o2).removeAll(beMixed)));
                    } else {
                        diff.put(field.getName(), Different.getInstance(o1, o2));
                    }

                }
            }
            boolean deepEquals = deepEquals(o1, o2);
            if (!deepEquals) {
                diff.put(field.getName(), Different.getInstance(o1, o2));
            }
        }
        return diff;
    }


    private static boolean deepEquals(Object source, Object target) {
        if (source instanceof Collection && target instanceof Collection) {
            return Objects.deepEquals(((Collection) source).toArray(), ((Collection) target).toArray());
        }
        return Objects.deepEquals(source, target);
    }

    private static Object getBeMixed(Object source, Object target) {
        Collection<Object> sourceCollection = (Collection<Object>) source;
        Collection<Object> targetCollection = (Collection<Object>) target;
        return sourceCollection.stream().filter(targetCollection::contains).collect(Collectors.toList());
    }


    @Data
    static class Different {
        private Object source;
        private Object target;

        public static Different getInstance(Object source, Object target) {
            Different different = new Different();
            different.source = source;
            different.target = target;
            return different;
        }
    }

标签:段值,Java,target,Object,Collection,source,Bean,o2,o1
From: https://www.cnblogs.com/lyuSky/p/16912340.html

相关文章

  • java静态代码块之扑克牌
    静态代码块生成54张扑克牌 packagecn.edu.dcxy;importjava.util.ArrayList;publicclassStaticCards{publicstaticArrayList<String>cards=newArra......
  • java工具类之验证码工具类
    生成验证码之工具类packagecn.edu.dcxy;importjava.util.Random;publicclassItUtil{/***由于工具类无需创建对象,所以把其构造器私有化*/......
  • Java并发编程学习12-任务取消(上)
    任务取消(上)《任务取消》由于篇幅较多,拆分了两篇来介绍各种实现取消和中断的机制,以及如何编写任务和服务,使它们能对取消请求做出响应。如何理解任务是可取消的?如果外部......
  • 资深java面试题及答案整理
     8.如果你的Serializable类包含一个不可序列化的成员,会发生什么?你是如何解决的?任何序列化该类的尝试都会因NotSerializableException而失败,但这可以通过在Java中为st......
  • JavaScript:世界上误解最深的语言
    JavaScript:TheWorld'sMostMisunderstoodProgrammingLanguageJavaScript:世界上误解最深的语言DouglasCrockfordwww.crockford.com翻译:袁晓辉JavaScript,akaMocha,a......
  • mybatis查询结果封装javabean属性时属性名与数据库字段不同都能封装????
    问题现象:通过mybatis查询数据库中的表记录,封装成javaBean,本来属性名和字段名不相同的情况下,属性最后应该注入null的,结果能够正常注入。javaBean:publicclassEmplo......
  • [收藏]用JavaScript让弹出页面以最小化的形式出现在状态栏中
    用JAVASCRIPT就行了:假设你要打开A.HTM,弹出B.HTM、C.HTM,可以这样写:A.HTM加上---------------<HEAD> ....... <SCRIPTLANGUAGE="JavaScript"> window.open('b.htm','_bl......
  • 万字详解 Java 线程安全,面试必备!
    来源:blog.csdn.net/u014454538/article/details/985158071.Java中的线程安全Java线程安全:狭义地认为是多线程之间共享数据的访问。Java语言中各种操作共享的数据有5种......
  • Javascript Event Loop
    JavascriptEventLoop是什么Javascript执行的时候是单线程的,所以一些耗时的操作需要启用其它的线程去处理,这样才不会阻塞整个线程的执行,这种任务我们称为异步任务。然后......
  • 第十四届蓝桥杯模拟赛第一期试题【Java解析】
    目录​​A二进制位数​​​​问题描述​​​​答案提交​​​​参考答案​​​​解析​​​​B晨跑​​​​问题描述​​​​答案提交​​​​参考答案​​​​解析​​​......