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;
        }
  • 相关阅读:
    Java实现 蓝桥杯VIP 算法训练 一元三次方程
    Java实现 蓝桥杯VIP 算法训练 乘法表
    Java实现 蓝桥杯VIP 算法训练 矩阵加法
    Java实现 蓝桥杯VIP 算法训练 一元三次方程
    Java实现 蓝桥杯VIP 算法训练 平方计算
    Java实现 蓝桥杯VIP 算法训练 平方计算
    Java实现 蓝桥杯VIP 算法训练 平方计算
    Java实现 蓝桥杯VIP 算法训练 乘法表
    Java实现 蓝桥杯VIP 算法训练 乘法表
    监管只是压倒网盘业务的一根稻草,但不是主要原因(答案只有一个:成本!)
  • 原文地址:https://www.cnblogs.com/ooo0/p/12582053.html
Copyright © 2011-2022 走看看