java Map 複製和物件的複製
A:淺複製(淺克隆): 淺複製僅僅複製所考慮的物件,而不復制它所引用的物件。
b:深複製(深克隆):深複製把要複製的物件所引用的物件都複製了一遍。
序列化和反序列化 Serializable
序列化的物件要實現Serializable接口才能實現序列化。
序列化後,通過反序列化可以得到和當前物件一樣的物件。
@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;
}
程式案例:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ObjectCp {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("*************關於Map的複製****************");
cpmap();
System.out.println("*************關於物件的複製****************");
objus();
}
public static void objus()
{
user ur= new user(1,"qq",23,"ww");
user ur2=ur;
System.out.println("資料展示");
System.out.println(ur.toString());
System.out.println(ur2.toString());
System.out.println("修改屬性值後資料展示");
ur.setAge(88);
System.out.println(ur.toString());
System.out.println(ur2.toString());
System.out.println("序列化後修改屬性值後資料展示");
ur2= clone(ur);
ur.setAge(11);
System.out.println(ur.toString());
System.out.println(ur2.toString());
}
public static void cpmap()
{
String str="ppppp";
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("str", str);
//放物件
map.put("list", list);
HashMap<String,Object> mapNew = new HashMap<String,Object>();
mapNew.putAll(map);//只對 基本型別和String 型別有效,物件型別無效
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);
mapNew.put("str", "wwwwwwww");
System.out.println(map);
System.out.println(mapNew);
System.out.println("----使用序列化進行深拷貝-----");
mapNew = clone(map);
list.add(400);
map.put("str", "mmmmmmm");
System.out.println(map);
System.out.println(mapNew);
System.out.println("----使用序列化進行深拷貝-----");
HashMap<String,Object> mapNews = new HashMap<String,Object>();
mapNews = clone(map);
list.add(500);
mapNews.put("str", "wwwwwwww");
System.out.println(map);
System.out.println(mapNews);
}
@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;
}
}
class user implements Serializable{
user(int uid,String uname,int age,String up)
{
this.uid=uid;
this.uname=uname;
this.age=age;
this.up=up;
}
private int uid;
private String uname;
private int age;
private String up;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUp() {
return up;
}
public void setUp(String up) {
this.up = up;
}
public String toString()
{
return String.format("uid:%d,uname:%s,age:%d,up:%s",uid,uname,age,up);
}
}
結論:
1.Map 物件 使用putall 可是實現對基本型別和String型別的深度複製,對引用型別必須通過序列化與反序列化處理。
2.物件間的複製,使用序列化進行深度複製。