java map深拷贝_关于HashMap的浅拷贝深拷贝

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 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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值