美文网首页
关于HashMap的浅拷贝深拷贝

关于HashMap的浅拷贝深拷贝

作者: 何止搬砖工 | 来源:发表于2020-07-08 13:07 被阅读0次

Map中存储的都是String,不是基本数据类型的,这个有很大影响;
putAll()和clone()对基本数据类型的数据是没有问题的,但是引用类型还是不行。

HashMap的浅拷贝clone()方法

一、创建一个HashMap对象source,进行赋值,然后再克隆一个对象targetMap出来。通过改变targetMap的值,观察source与targetMap的变化。

       HashMap source = new HashMap();
        source.put("key1", "value1");
        source.put("key2", "value2");
        for (Iterator keyItr = source.keySet().iterator(); keyItr.hasNext(); ) {
            Object key = keyItr.next();
            System.out.println("source " + key + " : " + source.get(key));
        }

        Map targetMap = (HashMap) source.clone();

        for (Iterator keyItr = targetMap.keySet().iterator(); keyItr.hasNext(); ) {
            Object key = keyItr.next();
            System.out.println("targetMap " + key + " : " + source.get(key));
        }

        System.out.println("---------------- change  targetMap key1 value: targetMap.put(\"key1\", \"modify\") ----------------");
        targetMap.put("key1", "modify");


        for (Iterator keyItr = source.keySet().iterator(); keyItr.hasNext(); ) {
            Object key = keyItr.next();
            System.out.println("source " + key + " : " + source.get(key));
        }

        for (Iterator keyItr = targetMap.keySet().iterator(); keyItr.hasNext(); ) {
            Object key = keyItr.next();
            System.out.println("targetMap " + key + " : " + source.get(key));
        }

结果:从打印可以看出,targetMap 的值并没有发生改变,跟source的值保持一致。

source key1 : value1
source key2 : value2
targetMap key1 : value1
targetMap key2 : value2
---------------- change  targetMap key1 value: targetMap.put("key1", "modify") ----------------
source key1 : value1
source key2 : value2
targetMap key1 : value1
targetMap key2 : value2

二、创建一个HashMap对象source,进行赋值,然后再克隆一个对象targetMap出来。通过改变source的值,观察source与targetMap的变化。

System.out.println("---------------- change  source key1 value: source.put(\"key1\", \"modify\") ----------------");
 source.put("key1", "modify");

结果:从打印可以看出,targetMap 的值会随着source的改变而改变,与source的值保持一致。(因为是引用类型)

source key1 : value1
source key2 : value2
targetMap key1 : value1
targetMap key2 : value2
---------------- change  source key1 value: source.put("key1", "modify") ----------------
source key1 : modify
source key2 : value2
targetMap key1 : modify
targetMap key2 : value2

三、创建一个HashMap对象source,进行赋值,然后再克隆一个对象targetMap出来。往targetMap增加键值对,观察source与targetMap的变化。

 System.out.println("---------------- change  targetMap key1 value: targetMap.put(\"key3\", \"value3\"); ----------------");
 targetMap.put("key3", "value3");

结果:从打印可以看出,targetMap 的值不会发生改变,与source的值保持一致。反而增加了一个键值对,但是值为null。

source key1 : value1
source key2 : value2
targetMap key1 : value1
targetMap key2 : value2
---------------- change  targetMap key1 value: targetMap.put("key3", "value3"); ----------------
source key1 : value1
source key2 : value2
targetMap key1 : value1
targetMap key2 : value2
targetMap key3 : null

HashMap的深拷贝方法

    @SuppressWarnings("unchecked")
    public static <T extends Serializable> T clone(T obj) {
        T clonedObj = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(obj);
            oos.close();
            ByteArrayInputStream bais = new  ByteArrayInputStream(baos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bais);
            clonedObj = (T) ois.readObject();
            ois.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return clonedObj;
    }

相关文章

网友评论

      本文标题:关于HashMap的浅拷贝深拷贝

      本文链接:https://www.haomeiwen.com/subject/cteqcktx.html