zoukankan      html  css  js  c++  java
  • 引用对象深度复制

    import java.util.List;
    
    public class BeanUtils {
        /**
         * @param orig 源对象
         * @param dest 目标对象
         */
        public static void copyProperties(final Object orig, final Object dest) {
            try {
                org.apache.commons.beanutils.BeanUtils.copyProperties(dest, orig);
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            }
        }
    
        /**
         * @param origs           源list对象
         * @param dests           目标list对象
         * @param origsElementTpe 源list元素类型对象
         * @param destElementTpe  目标list元素类型对象
         * @param <T1>            源list元素类型
         * @param <T2>            目标list元素类型
         * @Description:拷贝list元素对象,将origs中的元素信息,拷贝覆盖至dests中
         */
        public static <T1, T2> void copyProperties(final List<T1> origs, final List<T2> dests, Class<T1> origsElementTpe, Class<T2> destElementTpe) {
            if (origs == null || dests == null) {
                return;
            }
            if (dests.size() != 0) {
                //防止目标对象被覆盖,要求必须长度为零
                throw new RuntimeException("目标对象存在值");
            }
            try {
                for (T1 orig : origs) {
                    T2 t = destElementTpe.newInstance();
                    dests.add(t);
                    copyProperties(orig, t);
                }
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            }
        }
    }

    调用方法:
    List<A> aList = new ArrayList<>();
    aList.add(new A());
    List<A> aList2 = new ArrayList<>();
    BeanUtils.copyProperties(aList, aList2, A.class, A.class);
  • 相关阅读:
    高程图 GridMap
    VINS-Mono代码分析与总结(二) 系统初始化
    IMU误差模型与校准
    VINS-Mono代码分析与总结(一) IMU预积分
    XJTU 大一上
    iOS路由最佳选择是什么
    正向代理、反向代理、透明代理
    centos7国内镜像glbc版安装
    IntelliJ idea 中使用Git
    Mongo DB 2.6 需要知道的一些自身限定
  • 原文地址:https://www.cnblogs.com/nvsky/p/13995574.html
Copyright © 2011-2022 走看看