beat365体育-beat365体育官方网站-365体育app官方版下载

Java Map深度复制

Map深度复制 目录 Map深度复制 1,new HashMap(Object); 2,Put All; 3,串行化复制; 总结: 如何对Map类型的数据进行深度复制,有几种方式呢? 1,new Hash

Java Map深度复制

Map深度复制

目录

Map深度复制

1,new HashMap<>(Object);

2,Put All;

3,串行化复制;

总结:

如何对Map类型的数据进行深度复制,有几种方式呢?

1,new HashMap<>(Object);

代码:

public static void main(String[] args) throws IOException, ClassNotFoundException {

testNewMap();

}

public static void testNewMap(){

System.out.println("====== new HashMap<>(e) =======");

Map one = new HashMap<>();

one.put("one","fish");

Map two = new HashMap<>(one);

System.out.println("init: one: "+ one.toString());

System.out.println("init: two: "+ two.toString());

System.out.println("====== change to grass =======");

two.put("one","grass");

System.out.println("change: one: "+ one.toString());

System.out.println("change: two: "+ two.toString());

System.out.println();

}

使用 new HashMap<>(one) 的方式

结果:

====== new HashMap<>(e) =======

init: one: {one=fish}

init: two: {one=fish}

====== change to grass =======

change: one: {one=fish}

change: two: {one=grass}

2,Put All;

代码:

public static void main(String[] args) throws IOException, ClassNotFoundException {

testPutAllMap();

}

public static void testPutAllMap(){

System.out.println("====== put all map =======");

Map one = new HashMap<>();

one.put("one","fish");

Map two = new HashMap<>();

two.putAll(one);

System.out.println("init: one: "+ one.toString());

System.out.println("init: two: "+ two.toString());

System.out.println("====== change to monkey =======");

two.put("one","monkey");

System.out.println("change: one: "+ one.toString());

System.out.println("change: two: "+ two.toString());

System.out.println();

}

结果:

====== put all map =======

init: one: {one=fish}

init: two: {one=fish}

====== change to monkey =======

change: one: {one=fish}

change: two: {one=monkey}

这边会标黄,处理后会变成第一种的情况。

3,串行化复制;

代码:

public static void main(String[] args) throws IOException, ClassNotFoundException {

testDeepCopyMap();

}

public static void testDeepCopyMap() throws IOException, ClassNotFoundException {

System.out.println("====== DeepCopy Map =======");

Map one = new HashMap<>();

one.put("one","fish");

Map two = CloneUtils.cloneMap(one);;

System.out.println("init: one: "+ one.toString());

System.out.println("init: two: "+ two.toString());

System.out.println("====== change to snake =======");

two.put("one","snake");

System.out.println("change: one: "+ one.toString());

System.out.println("change: two: "+ two.toString());

System.out.println();

}

串行化代码:

public class CloneUtils {

public static T clone(T src) throws IOException, ClassNotFoundException {

ObjectInputStream ois = getInputStream(src);

//返回生成的新对象

@SuppressWarnings("unchecked")

T cloneObj = (T) ois.readObject();

ois.close();

return cloneObj;

}

public static List deepCopy(List src) throws IOException, ClassNotFoundException {

ObjectInputStream ois = getInputStream(src);

@SuppressWarnings("unchecked")

List dest = (List) ois.readObject();

ois.close();

return dest;

}

public static Map cloneMap(Map src) throws IOException, ClassNotFoundException {

ObjectInputStream ois = getInputStream(src);

@SuppressWarnings("unchecked")

Map result = (Map) ois.readObject();

ois.close();

return result;

}

private static ObjectInputStream getInputStream(Object src) throws IOException, ClassNotFoundException {

//写入字节流

ByteArrayOutputStream out = new ByteArrayOutputStream();

ObjectOutputStream obs = new ObjectOutputStream(out);

obs.writeObject(src);

obs.close();

//分配内存,写入原始对象,生成新对象

ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray());

return new ObjectInputStream(ios);

}

}

结果:

====== DeepCopy Map =======

init: one: {one=fish}

init: two: {one=fish}

====== change to snake =======

change: one: {one=fish}

change: two: {one=snake}

总结:

在使用Map复制的时候,一般用第一种的方式,如果大量使用,也可以考虑用第三种方式。

← 上一篇: 测试服今日改动:刀妹被动机制更新,乌鸦加强,沃里克调整
下一篇: 社区居委会工资待遇怎么样 →

相关推荐

【海信 TLM3226液晶电视】Hisense TLM3226液晶电视

【海信 TLM3226液晶电视】Hisense TLM3226液晶电视

数码首页 | 新浪首页 | 新浪导航 家电首页 > 家电大全 > > 海信 > TLM3226 查看全部产品图片 上传我的家电图片 海信 TLM3226 添加到对比栏 上市时间

C罗为什么总能靠假摔获得点球

C罗为什么总能靠假摔获得点球

C罗打进点球以后对西班牙球员诡秘一笑,没办法点球就是为C罗而定制的。 2014世界杯葡萄牙对伊朗的比赛中,C罗再次假摔获得点球,由于这个

新一期中国国家队全家福

新一期中国国家队全家福

新一期中国国家队全家福 2018-10-11 12:03:05 1/25 北京时间10月13日和16日,中国国家男子足球队将分别在苏州和南京进行两场热身赛。昨日,全体队

掌酷影院为什么看不了(掌酷影院无法观看原因解析及解决方案)

掌酷影院为什么看不了(掌酷影院无法观看原因解析及解决方案)

摘要: 随着互联网技术的飞速发展,在线视频平台已经成为人们日常生活的重要组成部分,掌酷影院作为一款流行的在线视频应用,为广大用户

matlab分段函数

matlab分段函数

在 MATLAB 中,定义分段函数可以使用 piecewise 函数或者条件语句(如 if、else)来实现。以下是两种常见方法的示例: 1. 使用 piecewise 函数(适用

[射门集锦]B组:葡萄牙3-3西班牙 比赛集锦

[射门集锦]B组:葡萄牙3-3西班牙 比赛集锦

首页 直播/赛程 时间赛程 球队赛程 小组赛赛程 淘汰赛赛程 VIP 视频 完整回放 进球大全 360度VR观赛 豪门盛宴 我爱世界杯 星耀金杯 世界杯最前