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);
  • 相关阅读:
    elasticsearch + fluentd + kibana 日志收集
    redis-dump数据导入导出
    zabbix 存储数据清理
    etcd集群部署
    yum安装zabbix4.2
    U盘启动安装CentOS 7出现 -dracut initqueue timeout
    About && 友链
    红队-C2 Server基础构建
    从0学习WebLogic CVE-2020-2551漏洞
    代码审计-phpok框架5.3注入漏洞
  • 原文地址:https://www.cnblogs.com/nvsky/p/13995574.html
Copyright © 2011-2022 走看看