zoukankan      html  css  js  c++  java
  • java 克隆

    方法一:实现 Cloneable 接口并重写 Object 类中的 clone() 方法

    import lombok.Data;
    
    public class Test {
    
        @Data
        static class Hello implements Cloneable {
            private String name;
    
            public Hello(String name) {
                this.name = name;
            }
    
            @Override
            protected Object clone() throws CloneNotSupportedException {
                return super.clone();
            }
        }
    
        public static void main(String[] args) throws CloneNotSupportedException {
            Hello hello = new Hello("name");
            Hello clone = (Hello) hello.clone();
            System.out.println(clone);
        }
    
    }

    方法二:使用spring的BeanUtils(推荐)

    import lombok.Data;
    import org.springframework.beans.BeanUtils;
    
    public class Test {
    
        @Data
        static class Hello {
            private String name;
    
            public Hello() {
            }
    
            public Hello(String name) {
                this.name = name;
            }
        }
    
        public static void main(String[] args) {
            Hello source = new Hello("name");
            Hello target = new Hello();
    
            BeanUtils.copyProperties(source, target);
            System.out.println(target);
        }
    
    }

    方法三:使用fastjson通过对象json序列化和反序列化来完成对象复制(深度克隆)

    import com.alibaba.fastjson.JSON;
    import lombok.Data;
    
    public class Test {
    
        @Data
        static class Hello {
            private String name;
    
            public Hello() {
            }
    
            public Hello(String name) {
                this.name = name;
            }
        }
    
        public static void main(String[] args) {
            Hello source = new Hello("name");
    
            String sourceStr = JSON.toJSONString(source);//序列化为json
            Hello target = JSON.parseObject(sourceStr, Hello.class);//反序列化
            System.out.println(target);
        }
    
    }

    方法四:利用序列化实现对象的拷贝(深度克隆)

      /**
         * 对象深拷贝
         *
         * @param obj
         * @param <T>
         * @return
         */
        public static <T extends Serializable> T deepClone2(T obj) {
            T cloneObj = null;
            try {
                //写入字节流
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                ObjectOutputStream obs = new ObjectOutputStream(out);
                obs.writeObject(obj);
                obs.close();
    
                //分配内存,写入原始对象,生成新对象
                ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray());
                ObjectInputStream ois = new ObjectInputStream(ios);
                //返回生成的新对象
                cloneObj = (T) ois.readObject();
                ois.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                //关闭流
            }
            return cloneObj;
        }
    
        /**
         * 对象深拷贝 try-with-resources写法
         *
         * @param obj
         * @param <T>
         * @return
         */
        public static <T extends Serializable> T deepClone(T obj) {
            T cloneObj = null;
            try (ByteArrayOutputStream out = new ByteArrayOutputStream();
                 ObjectOutputStream obs = new ObjectOutputStream(out);
                 ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray());
                 ObjectInputStream ois = new ObjectInputStream(ios);
            ) {
                obs.writeObject(obj);
                cloneObj = (T) ois.readObject();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            return cloneObj;
        }
    
        /**
         * List深拷贝
         *
         * @param src
         * @param <T>
         * @return
         */
        public static <T> List<T> deepCloneList2(List<T> src) {
            List<T> dest = null;
            try {
                ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
                ObjectOutputStream out = new ObjectOutputStream(byteOut);
                out.writeObject(src);
    
                ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
                ObjectInputStream in = new ObjectInputStream(byteIn);
                dest = (List<T>) in.readObject();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                //关闭流
            }
            return dest;
        }
    
        /**
         * List深拷贝 try-with-resources写法
         *
         * @param src
         * @param <T>
         * @return
         */
        public static <T> List<T> deepCloneList(List<T> src) {
            List<T> dest = null;
            try {
                ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
                ObjectOutputStream out = new ObjectOutputStream(byteOut);
                out.writeObject(src);
    
                ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
                ObjectInputStream in = new ObjectInputStream(byteIn);
                dest = (List<T>) in.readObject();
            } catch (Exception e) {
                e.printStackTrace();
            }return dest;
        }
  • 相关阅读:
    【转】Loadrunder场景设计篇——添加windows Resource计数器和指标说明
    【转】基于Selenium的web自动化框架(python)
    测试用例
    向SQL Server中导入Excel的数据
    SQl server更新某阶段的匹配关系。
    Python -- print(dataframe)时,省略部分列。
    Python -- Pandas介绍及简单实用【转】
    sqlserver清除缓存,记录查询时间
    ArcMap 10.2 crashes during Loading Document launch stage
    PYTHON:HTTP头设置工具(以附件名为例)
  • 原文地址:https://www.cnblogs.com/ooo0/p/12582053.html
Copyright © 2011-2022 走看看