标签:更改 ble ace 拷贝 string 实现 exception put 变化
HashMap<String,Object> hm = new HashMap(); HashMap<String,Object> hmCopy = new HashMap(); hm.put("123", 123); System.out.println(hm.get("123")); hmCopy = hm; hmCopy.remove("123"); System.out.println(hm.get("123")); 输出结果:123 null
//map拷贝putAll方法: HashMap<String,Object> hm = new HashMap(); HashMap<String,Object> hmCopy = new HashMap(); hm.put("123", 123); System.out.println(hm.get("123")); hmCopy.putAll(hm); hmCopy.remove("123"); System.out.println(hm.get("123")); 输出结果:123 123
List<Integer> list = new ArrayList<Integer>(); list.add(100); list.add(200); HashMap<String,Object> map = new HashMap<String,Object>(); map.put("basic", 100);//放基本类型数据 map.put("list", list);//放对象 HashMap<String,Object> mapNew = new HashMap<String,Object>(); mapNew.putAll(map); System.out.println("----数据展示-----"); System.out.println(map); System.out.println(mapNew); System.out.println("----更改基本类型数据-----"); map.put("basic", 200); System.out.println(map); System.out.println(mapNew); System.out.println("----更改引用类型数据-----"); list.add(300); System.out.println(map); System.out.println(mapNew); System.out.println("----使用序列化进行深拷贝-----"); mapNew = CloneUtils.clone(map); list.add(400); System.out.println(map); System.out.println(mapNew); 输出结果: ----数据展示----- {basic=100, list=[100, 200]} {basic=100, list=[100, 200]} ----更改基本类型数据----- {basic=200, list=[100, 200]} {basic=100, list=[100, 200]} ----更改引用类型数据----- {basic=200, list=[100, 200, 300]} {basic=100, list=[100, 200, 300]} ----使用序列化进行深拷贝----- {basic=200, list=[100, 200, 300, 400]} {list=[100, 200, 300], basic=200}
//附克隆方法: public static <T extends Serializable> T clone(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(); } return cloneObj; }
标签:更改 ble ace 拷贝 string 实现 exception put 变化
原文地址:http://www.cnblogs.com/leskang/p/7169233.html